0

I have a few lines of text like this:

abc
def
ghi

and I want to assign these multiple lines to a Matlab variable for further processing.

I am copying these from very large text file and want to process it in Matlab Instead of saving the text into a file and then reading line by line for processing.

I tried to handle the above text lines as single string but am getting an error whilst trying to assign to a variable:

x = 'abc
def
ghi'

Error:

x = 'abc
        |
Error: String is not terminated properly.

Any suggestions which could help me understand and solve the issue will be highly appreciated.

Wolfie
  • 27,562
  • 7
  • 28
  • 55
User1551892
  • 3,236
  • 8
  • 32
  • 52

2 Answers2

3

I frequently do this, namely copy text from elsewhere which I want to hard-code into a MATLAB script (in my case it's generally SQL code I want to manipulate and call from MATLAB).

To achieve this I have a helper function in clipboard2cellstr.m defined as follows:

function clipboard2cellstr

str = clipboard('paste');
str = regexprep(str, '''', ''''''); % Double any single quotes
strs = regexp(str, '\s*\r?\n\r?', 'split');
cs = sprintf('{\n''%s''\n}', strjoin(strs, sprintf('''\n''')));
clipboard('copy', cs);
disp(cs)
disp('(Copied to Clipboard)')

end

I then copy the text using Ctrl-c (or however) and run clipboard2cellstr. This changes the contents of the clipboard to something I can paste into the MATLAB editor using Ctrl-v (or however).

For example, copying this line

and this line

and this one, and then running the function generates this:

{
'For example, copying this line'
'and this line'
'and this one, and then running the function generates this:'
}

which is valid MATLAB which can be pasted directly in.

Justin
  • 1,980
  • 1
  • 16
  • 25
2

Your error is because you ended the line when MATLAB was expecting a closing quote character. You must use array notation to have multi-line or multi-element arrays.


You can assign like this if you use array notation

x = ['abc'
     'def'
     'hij']
>> x = 3×3 char array

Note: with this method, your rows must have the same number of characters, as you are really dealing with a character array. You can think of a character array like a numeric matrix, hence why it must be "rectangular".

If you have MATLAB R2016b or newer, you can use the string data type. This uses double quotes "..." rather than single quotes '...', and can be multi-line. You must still use array notation:

x = ["abc"
     "def"
     "hijk"]
>> x = 3×1 string array

We can have different numbers of characters in each line, as this is simply a 3 element string array, not a character array.

Alternatively, use a cell array of character arrays (or strings)

x = {'abc'
     'def'
     'hijk'}
>> x = 3×1 cell array

Again, you can have character arrays or strings of different lengths within a cell array.


In all of the above examples, a newline is simply for readability and can be replaced by a semi-colon ; to denote the next line of the array.


The option you choose will depend on what you want to do with the text. If you're reading from a file, I would suggest the string array or the cell array, as they can deal with different length lines. For backwards compatibility, use a cell array. You may find cellfun relevant for operating on cell arrays. For native string operations, use a string array.

Wolfie
  • 27,562
  • 7
  • 28
  • 55
  • I have approx 10 text lines and copying them from file and want to avoid to add '' or "" in each line. I want to find a way to bypass this step. – User1551892 Feb 19 '18 at 12:23
  • I want to repeat this process again and again. Copy some part of huge file and process it and repeat it... – User1551892 Feb 19 '18 at 12:29
  • You can try `textscan` – Adiel Feb 19 '18 at 12:32
  • Sounds like you're copy and pasting lines of your file into your Matlab code? Simply read in the file (using `textscan` as Adiel suggests or perhaps `fscanf`) and append to one of the above array types as needed. You shouldn't have to manually add `' '` or `" "` when reading from a file, these just denote which variable type you're dealing with in the examples / when using manual input. – Wolfie Feb 19 '18 at 12:38