Unable to concatenate the table variables

13 views (last 30 days)
ubaidulla
ubaidulla on 12 Jun 2022
Edited: Walter Roberson on 13 Jun 2022
I am using readtable command to read data from .csv file as given below. One of the column contains varying quantity, and I would like to specify those values in commandwindow. How can I achieve this.
In the below code, I am getting error saying ''Unable to concatenate the table variables''. How to resolve this?
Thank you
R1 = 0.02; R2 = 0.03;
my_data_1 = readtable('my_data.csv')
my_data_2 = table2array(my_data_1(:,1:4));
  4 Comments
Stephen23
Stephen23 on 13 Jun 2022
"That is values assigned to R1 and R2 are of class 'complex'. "
MATLAB does not have a "complex" class type. Here are the fundamental MATLAB classes:
R1 could be char, or string, or categorical, or something similar.
Walter Roberson
Walter Roberson on 13 Jun 2022
The code is expecting that when the file is read, that any variable names in the text will be replaced with the value of the variable. However, MATLAB does not provide any file reading routines that will do that.
If you read the entire file as a character vector, and put '[' before and ']' after, and evalc() then possibly you would get what you are after.
... At least until someone puts
system('format C:\')
into an input file for the lulz.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 12 Jun 2022
Your Z column contains text but your other columns are numeric. It is not possible to create a single array that contains both text and numeric, other than converting to a cell array such as by using table2cell()
Note: when you assign values to variables, such as the way you assign to R1 and R2 here, and then you try to read from a file that contains text that matches variable names... then the values of the MATLAB variables will not be automatically substituted where the text occurs.
Also note that Excel has no way to represent complex numbers, so the 0.01+0.1j can only be read in as text.
  2 Comments
ubaidulla
ubaidulla on 13 Jun 2022
Edited: ubaidulla on 13 Jun 2022
Thank you for your response. Here
"Also note that Excel has no way to represent complex numbers, so the 0.01+0.1j can only be read in as text."
For instance, if I change the contents of column Z in file "my_data.csv", as follows:
0.04+0.3j
0.04+0.4j
0.01+0.1j
0.02+0.2j
and then if I run the code
my_data_1 = readtable('my_data.csv')
my_data_2 = table2array(my_data_1(:,1:4));
Y=1./my_data_2(:,4)
then my result will be
Y =
0.43668 - 3.2751i
0.24752 - 2.4752i
0.9901 - 9.901i
0.49505 - 4.9505i
That means 'complex' data can be read from csv file.
My question is 'is it possible to use a variable name inside .CSV file and assign its value in matlab commandwindow?
Thank you

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!