How to read value from spreadsheet while using symbolic math tool?

2 views (last 30 days)
How to read the values of A0 and A1 from the spreadsheet where A0 and A1 have more than one values? A1 has real and imaginary part which are in separate columns in the spreadsheet.
For example, the following code has fixed (single) value for A0 and A1, but what if multiple values are to be read:
Q = @(v) sym(v);
A0 = Q('1.5207');
A1 = Q('0.853721-1.816893i');
Note: Quote ('') has significant effect in this case, see below.

Answers (2)

halleyhit
halleyhit on 25 Jun 2024
I think you need to convert char to value first, and then, value to sym. So the code may look like
input = '0.853721-1.816893i';
eval(['temp =' input]);
temp = 0.8537 - 1.8169i
A1=sym(temp)
A1 = 
  1 Comment
KS
KS on 25 Jun 2024
Edited: KS on 25 Jun 2024
Thanks @halleyhit.
You mentioned [input = '0.853721-1.816893i';] directly, but what if the values are to be read from the excel file through a variable like x=0.853721, y=1.816893 and input=x-iy. In that case, what'd be the solution ?

Sign in to comment.


Aman
Aman on 25 Jun 2024
Hi KS,
From the information I understood, you want to read A0 and A1 data from a spread sheet and then perform some operations.
Considering your table looks like below, you can read the information from the table in a similar fashion to what has been done in the code snippet attached.
data = readtable('testing.xlsx',"ReadRowNames",true);
Q = @(v) sym(v);
for i=1:height(data)
dt = data(i,:);
A0 = Q(string(dt.Row{1}));
A1 = Q(num2str(dt.Var1) + string(dt.Var2{1}));
disp(A0-A1); %Can be any operation
end
Coming to your second question, the reason you are seeing a different output when using a quote is because when you pass a numeric value to the "sym" function, it converts to a symbolic number with some approximation, while when you pass a string, it converts to an accurate symbolic number without approximation. So it is always better to keep the argument type the same while doing some operations with symbolic numbers. You can refer to the below link to read more about it:
I hope this helps!

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!