I want to export matlab output to an excel file starting from G2 column and for this the code i had written is exporting the data correctly but not to the desired location .It is printing in 21st number of rows rather than 2nd.

1 Ansicht (letzte 30 Tage)
the code is
ResultFile = xlsread('filename');
sz= size(ResultFile,1);
b= num2str(sz+1);
location = strcat('G2',b);
fprintf('value in location is %g\n',location);
xlswrite('filename',fnlarray,'Sheet1',location);

Akzeptierte Antwort

Geoff Hayes
Geoff Hayes am 13 Jan. 2015
Himakshi - what is sz? From your code, it appears to be the number of rows in the data ResultFile. You then create a variable b from sz as
b= num2str(sz+1);
and then concatenate this to G2 as
location = strcat('G2',b);
So if sz is zero, the b is one, and so location is G21 which may explain the behaviour that you are observing.
The result of the fprintf should tell you which row of column G that you are are writing the data to. What does this line of code write to the command window? What are the dimensions of ResultFile and why, if you want to write the data to the second row of column G, are you creating this location variable?
If all you want to do is write the data at G2, then your code should simply be
xlswrite('filename',fnlarray,'Sheet1','G2');
In the above, presumably 'filename' is just a placeholder for the name of the file, and you are not referencing a local variable called filename (as this would be incorrect usage of this string).
  2 Kommentare
himakshi Shekhawat
himakshi Shekhawat am 13 Jan. 2015
Bearbeitet: Geoff Hayes am 13 Jan. 2015
I have run the code in loop so value of location for a loop of size 3 is
value in location is 71
value in location is 50
value in location is 52
value in location is 71
value in location is 50
value in location is 53
value in location is 71
value in location is 50
value in location is 54
In xlswrite if I give only G2 then it will overwrite the data for orevious iterations and print only the value for last iteration in a single row instead of 3 for loop size three
Geoff Hayes
Geoff Hayes am 13 Jan. 2015
Change your fprintf so that you are printing a string on each iteration of the loop. Try
fprintf('value in location is %s\n',location);
What is the output now? And since you are iterating over the above code, where do you want the other data (on say the second iteration) to go i.e.which column which row, etc.?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by