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 CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by