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

5 次查看(过去 30 天)
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.

回答(2 个)

halleyhit
halleyhit 2024-6-25
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 个评论
KS
KS 2024-6-25
编辑:KS 2024-6-25
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 ?

请先登录,再进行评论。


Aman
Aman 2024-6-25
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!

类别

Help CenterFile Exchange 中查找有关 Symbolic Math Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by