Brace indexing error maybe complex ?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
John Doe
am 6 Dez. 2020
Bearbeitet: John Doe
am 12 Dez. 2020
Hello everyone !
The famous brace indexing. I can't get ride of, i don't see where i do something wrong.
The main structure:
close all;
clearvars;
load Dataset\posture.mat;
signal=datablock1.data;
Fe=1000;
t=0:0.001:10;
%Create excel
Tremor_functions.excel;
%EMG analysis
for mrkr=1:6
Tremor_functions.Timestamp(markers,mrkr,Fe)
for muscle=1:4
Tremor_functions.signal_processing(Debut,Fin,muscle,signal,Fe,mrkr,t);
end
end
%Fill with data
Tremor_functions.fill_excel(folder,Answer)
First i create an excel and i create some column in it
function excel
Prompt={'Surname', 'Name', 'Birthday (xx xx xxxx)','Evaluation (yyyy yy yy)'};
Dlgtitle='Donnees patient';
Def={'John','Doe','01 01 1961','2015 02 16'};
Answer=inputdlg(Prompt,Dlgtitle,1,Def);
Info_patient={Answer{1},Answer{2},Answer{3}};
Eval_date={'Evaluation',Answer{4}};
e= Answer{4};
assignin('base','Answer',e);
Muscle={'Fl_Pr','Ex_Su','Bic','Tri'};
Taches=[1;2;3;4;5;6];
Taches_c={'Taches'};
folder = [Answer{1},' ',Answer{2},' ',num2str(Answer{3})];
assignin('base','folder',folder)
if ~exist(folder, 'dir')
mkdir(folder);
end
filename = 'Info_patient.xlsx';
assignin('base','filename',filename)
fullname = fullfile(folder, filename);
assignin('base','fullname',fullname)
xlswrite(fullfile(folder,Answer{4}),Info_patient,'Fpic','A2');
xlswrite(fullfile(folder,Answer{4}),Eval_date,'Fpic','A3');
xlswrite(fullfile(folder,Answer{4}),Muscle,'Fpic','B4');
xlswrite(fullfile(folder,Answer{4}),Taches,'Fpic','A5');
xlswrite(fullfile(folder,Answer{4}),Taches_c,'Fpic','A4');
end
At the end of my signal processing i have gathered information that i want to put in my excel sheet, i start with that:
function fill_excel(folder,Answer)
xlswrite(fullfile(folder,Answer{4}),Fpic,'Fpic','B5');
end
But when i run it, i have Brace indexing is not supported for variables of this type.
xlswrite(fullfile(folder,Answer{4}),Fpic,'Fpic','B5');
When i don't have something else. I have assigned some of variables, but it doesn't work.
If someone have a clue !
Thanks !
0 Kommentare
Akzeptierte Antwort
Stephen23
am 6 Dez. 2020
"I can't get ride of, i don't see where i do something wrong."
You assign the content of the fourth cell of Answer to variable e (so e is a character vector):
Answer=inputdlg(Prompt,Dlgtitle,1,Def);
...
e= Answer{4}; % e is a char vector
which you then magically make appear in a different workspace with the name Answer (do you see the problem now?)
assignin('base','Answer',e);
which you then try to access as if it was still the same Answer as in the first workspace:
..fill_excel(folder,Answer)
where inside fill_excel you try to access its fourth cell (but of course character vectors do not have cells):
xlswrite(fullfile(folder,Answer{4}),Fpic,'Fpic','B5')
% ^^^^^^ character vector
This is a classic example of obfuscated code that relies on assignin to make data magically jump between workspaces thus causing simple bugs that are then difficult to debug because of that pointless obfuscation. Much better** code would just pass the data reliably as input/output arguments, just as the MATLAB documentation recommends
** better in the sense simpler, faster, neater, more efficient, clearer, less buggy, and easier to debug.
5 Kommentare
Stephen23
am 12 Dez. 2020
"i just don't get it yet"
All of the different workspaces, assignin, etc. are just distractions and obfuscations that make it harder to understand the bug. But basically you are doing this:
Answer = {'cat','in','hat'}
Answer = Answer{2} % oops... same name, but no longer a cell array!
fullfile('whatever',Answer{2}) % not a cell array, so why use curly braces?
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Function Creation finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!