MATLAB: Writing multiple variables with eval into excel spreadsheet

anti-patternbuggycomplexevalexportslowvery complex

Hi, I am trying to write final variables that was analyzed 'eval' function into excel spreadsheet. Attached data contains 3 subjects folder with 3 trials each, and data table that I want to organize. When I run this matlab script that I made, you are able to see final variables what I want to export. But I had no idea how to export nicely (not manually) these variables into data table in the excel that I finally use.
I would really appreciate somebody help me to figure this issue.
Thanks.

Best Answer

"But I had no idea how to export nicely..."
You can't.
Doing anything "nicely" is impossible with that code.
Because you forced yourself into writing slow, complex, buggy, obfuscated code. Read this to know why:
Because you used eval to perform every single data manipulation, with lines like this:
eval(sprintf('[%s_%s_num %s_%s_char %s_%s_raw] = xlsread(''./%s/%s.csv'');',SubjectName{i},Order{j},SubjectName{i},Order{j},SubjectName{i},Order{j},SubjectName{i},Order{j}));
eval(sprintf('[frame,NoColumn]=size(%s_%s_num);',SubjectName{i},Order{j}));
eval(sprintf('%s_%s_Pos=%s_%s_num(15:end,1:%d);',SubjectName{i},Order{j},SubjectName{i},Order{j},NoColumn));
Start again. Do NOT use that code. There is no way you can ever do anything "nicely" after doing that. Using eval like that is like shooting yourself in the knee, and then asking us how to run a marathon: what advice do you expect to get, apart from "do not shoot yourself in the knee" ?
"I just wanted to ask what would be best way to export this condition"
The "best way" is to write better code (in the sense simpler, neater, more efficient) without using eval. Probably some simple indexing (of a ND array, a cell array, a structure, a table, etc) would be the key, although using structure fieldnames or a table are also options to consider.
Let me get you started:
SubjectName = {'C01','C02','C03'};
Order = {'Gait1','Gait2','Gait3'};
NR = numel(SubjectName);
NC = numel(Order);
num = cell(NR,NC); % preallocate



str = cell(NR,NC); % preallocate
raw = cell(NR,NC); % preallocate
pos = cell(NR,NC); % preallocate
for ii = 1:NR
for jj = 1:NC
fnm = fullfile('.',SubjectName{ii},sprintf('%s.csv',Order{jj}));
[num{ii,jj},str{ii,jj},raw{ii,jj}] = xlsread(fnm);
...
end
end
Note the indexing. Note no eval anywhere. This code will be much easier to work with (just use indexing), will be less buggy, and will be much much more efficient.