Copy results from a .mat file to existing excel file

5 views (last 30 days)
Hello, I have a .mat file (please check the attached script which produces it) as an output and I want to export the results into an existing excel sheet. Run1 and Run2 in the image are the results from previous simulations which are saved in .mat file everytime i run my script. I entered those 2 manually and I can't find a way to export by writing a script. The case names and number of outputs are same everytime, only the output changes. Thank you very much for your help. (Matlab version: R2018b)
  2 Comments
Maximilian Panzik
Maximilian Panzik on 17 Sep 2020
Hi there,
as I understand you need to continue an excel file, that is already populated.
So first you need to know, where to continue, so find out the size of the exising data:
[num,txt,raw] = xlsread(filename);
[rowN,colN] = size(raw);
You now have to define, where you start to write your data in the format 'A1'. If your max column is Z, you could use
range = [char(colN + 1 + 64),'1'];
Format your data in an array that equals your export format and use
xlswrite(filename,data,sheetName,range);
to write to your excel file.
Kezi Kns
Kezi Kns on 17 Sep 2020
Hello Max, as you said "Format your data in an array that equals your export format and use ". As the data in my .mat file is structured as "1 by 12 struct" and all the results are store in the column "evaluation" (which has further fields inside). I donot know how to extract this data and export it to excel. I have looked it up here and found that we cannot separate the fields from struct unless use "-struct" while saving the file. Thank you.

Sign in to comment.

Accepted Answer

Maximilian Panzik
Maximilian Panzik on 18 Sep 2020
You can use struct2array
sz = size(testSetup,2);
arrSz = sz*4;
exportArr=[];
for i = 1:sz
str = struct2array(testSetup(i).evaluation);
data = struct2array(str);
exportArr = [exportArr;nan;data']
end
This creates an array in the format of the data columns in your excel file.
  1 Comment
Kezi Kns
Kezi Kns on 18 Sep 2020
Edited: Kezi Kns on 16 Oct 2020
Thank you Max, it worked. I just have 2 questions if you don't mind.
1. Could you please explain what this line does, I undertand that it's finding the next available column in Excel but what is "1+64" ?
range = [char(colN + 1 + 64),'1'];
2. I have a different simulation where i should export data from .mat to excel similar to this one. I used the same script but it gave me an error " Conversion of struct from logical is not possible". I noticed that in the .mat file for all cases in evaluation there is a 1 by 1 logical. Is the error because of this logical ? If so how can i convert to array while skipping this logical.
Update Ans: Yes, the error is because of a logical that was present. I couldn't eliminate logical from testSetup, so i have found a way to avoid it while testSetup is being created. So it worked.
Thank you once again

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!