Error in importing the matlab ouput to excel file

i wrote the code for non linear equations and i got the result cubic roots. The problem is that i have to import the results to excel file . i use the command xlswrite but i am getting the error unable to convert the data. could anyone tell me to solve this issue. i am attaching my code below
clear all
close all
syms N
Qo=xlsread('suspended.xlsx','A:A')
R=xlsread('suspended.xlsx','B:B')
No=xlsread('suspended.xlsx','C:C')
X=xlsread('suspended.xlsx','D:D')
r = (No-N)./No ;
V = 2435 ;
k = 1.076 ;
Kn = 0.27 ;
nX = size(X,1 );
solutions = cell(nX,1 );
for S=1:nX
solutions{S} = solve(Qo(S) .* (1+R(S)) .* (No(S)-N) .* r(S) - V*((X(S)*k*N)./(Kn+N)), N );
vpa(solutions{S },5)
xlswrite(ans,'suspended.xlsx')

Answers (2)

It looks like you're trying to output a symbolic variable to excel, which I don't think is allowed. Try converting to double first.
vpa(solutions{S },5)
double(ans)
xlswrite(ans,'suspend.xlsx')

5 Comments

i am getting the error that the filename should be in string scalar and character vector.and one more problem is that the ans is taking the only last value
ans only contains the final value set from the loop because it is converted to a double outside of the loop. Each time you run the loop, the vpa command overwrites ans, until it retains the final value when it leaves the loop. ans is then overwritten by the double command, where the final vpa results are converted from sym class to double class values.
If you want to use each set of results from vpa then you need to move whatever command uses those results inside the loop, or add a command inside the loop that stores each set of results progressively.
for S=1:nX
...
results(:,S) = vpa(solutions{S },5); % Store results in a matrix
end
I believe the issue with xlswrite is simply an error in the order of the inputs. Check the linked documentation.
results(:,S) = double(vpa(solutions{S },5)); % Store results in a matrix
However: cubic roots often have complex-valued solutions, but excel does not support complex numbers.
I suppose you could convert to strings in that case. My little bit of research indicates it's not nearly as pretty, but it would be possible to break the results out into a string array which could be exported to excel.
WIth the particular data that the user posted, it happens that the roots are all real-valued (though some of them are negative.)

Sign in to comment.

syms N
Qo = xlsread('suspended.xlsx','A:A');
R = xlsread('suspended.xlsx','B:B');
No = xlsread('suspended.xlsx','C:C');
X = xlsread('suspended.xlsx','D:D');
r = (No-N)./No ;
V = 2435 ;
k = 1.076 ;
Kn = 0.27 ;
nX = size(X,1 );
solutions = cell(1, nX);
for S=1:nX
solutions{S} = solve(Qo(S) .* (1+R(S)) .* (No(S)-N) .* r(S) - V*((X(S)*k*N)./(Kn+N)), N );
end
sold = cell2mat(cellfun(@(C) round(double(C),5), solutions, 'uniform', false ));
sold
sold = 3×4
-1.7821 -1.0898 -0.8270 -0.6909 17.2852 33.1067 50.1456 67.9752 424.2270 467.7131 510.4115 552.4458
xlswrite('suspended.xlsx', sold)
This creates the roots as rows of the output, with the columns corresponding to original data rows form the xlsx file. Or it would be easy enough to write sold.' to have the columns be the roots and the rows correspond to the original data rows from the xlsx file.

Asked:

on 19 Jan 2021

Commented:

on 20 Jan 2021

Community Treasure Hunt

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

Start Hunting!