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

2 ビュー (過去 30 日間)
KS
KS 2024 年 6 月 25 日
コメント済み: KS 2024 年 6 月 27 日
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!
  1 件のコメント
KS
KS 2024 年 6 月 27 日
Though I couldn't solve the problem, btw many thanks for your effort

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by