Brace indexing error maybe complex ?

1 view (last 30 days)
John Doe
John Doe on 6 Dec 2020
Edited: John Doe on 12 Dec 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 !

Accepted Answer

Stephen23
Stephen23 on 6 Dec 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 Comments
Stephen23
Stephen23 on 12 Dec 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 = 1x3 cell array
{'cat'} {'in'} {'hat'}
Answer = Answer{2} % oops... same name, but no longer a cell array!
Answer = 'in'
fullfile('whatever',Answer{2}) % not a cell array, so why use curly braces?
Brace indexing is not supported for variables of this type.
John Doe
John Doe on 12 Dec 2020
Edited: John Doe on 12 Dec 2020
Super i understand much better now, you're a pedagogue champ' Stephen ! Thanks a lot !
I have much more question for u. I'll write those in others topic !

Sign in to comment.

More Answers (0)

Categories

Find more on Function Creation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!