Continue a statement into many lines without using ' ' ...

5 views (last 30 days)
Dear Experts,
I have a lengthy query in MySQL that I want to use it in Matlab. Matlab will use this to query the database (MySQL). However, I have to add ' ' and ... to each line of the query in Matlab like the example shown below:
Actual query is much longer than this. It is troublesome to type ' ' ... per line. Any other way that Matlab will recognize it as a single string?
sqlquery = [ 'SELECT ' ...
'r.id, ' ...
'r.wgt, ' ...
'r.date, ' ...
'b.price, ' ...
's.ipo ' ...
'FROM table1 AS r ' ...
'INNER JOIN table2 AS b ON r.id = b.id AND r.date = b.date ' ...
'INNER JOIN table2 AS s ON r.id = s.id AND r.date = 20160201 AND s.date=20130930' ...
];

Answers (2)

Walter Roberson
Walter Roberson on 12 Jun 2016
You should be considering using data files for constant strings that long.
  3 Comments
Walter Roberson
Walter Roberson on 12 Jun 2016
%read template from file
sqlquery = fileread('MyQueryTemplate#1.txt');
%replace any newlines with whitespace so it becomes all one line
mask = ismember(sqlquery, sprintf('\r\n'));
sqlquery(mask) = ' ';
%now use it
....
Guillaume
Guillaume on 13 Jun 2016
"I thought of excel"
Way overkill! Just use a plain text file as per Walter's example.

Sign in to comment.


Stephen23
Stephen23 on 12 Jun 2016
Edited: Stephen23 on 13 Jun 2016
Short answer:
no
Don't write it by hand anyway: for string that long it should be stored in a text file and read it into MATLAB using fileread.
Long answer: (for those who want to understand MATLAB
This is a necessary side effect of two things:
  1. all char variables are arrays (matrices).
  2. a convenience syntax is offered to make entering matrices easier.
Concatenation on multiple lines without the ellipses tells MATLAB that you want to create a matrix, and that each row that you write is a different row of the matrix. This is because MATLAB offers this convenience syntax for arrays:
>> [1,2
3,4
5,6]
ans =
1 2
3 4
5 6
or equivalently for a char array:
>> ['ab'
'cd'
'ef']
ans =
ab
cd
ef
This convenience syntax is really useful, because it allows copy-and-paste matrices to be input correctly! Imagine what would be required to make a copy-and-paste matrix work correctly, if your proposed syntax meant all values were put onto one line... What you are proposing would break this standard behavior because users who expect a matrix will suddenly get a vector. Ouch!
In languages which are limited to "one dimensional" lists, there is no difference in meaning between
[1
2]
or
[1,...
2]
because their semantic meaning is the same: join along one dimension, the only dimension they have (poor deprived languages). In MATLAB there must be some way of distinguishing between the intention: continuation along the same dimension, or starting the new dimension. If it would not be with ellipses, then something would have to be used in its place, so you would still have to type something...

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!