Problem with nested or local functions in GUIDE
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Michael Daldini
el 27 de Oct. de 2017
Comentada: Jan
el 5 de Nov. de 2017
Hi everyone, This is the Second question of a series of questions to complete a project. The First question can be found here . The Third question is accessible here.
I am building the GUI, right now I am at the point you can see in the picture and I am testing "à côté" small bunches of code that i subsequently integrate in the main GUI.
I am having problems to integrate the function that says what happens when the box is checked in the GUI (function boxchecked). In the code below it works fine but when I integrate it in my GUI code it says that some function were not closed with an end statement. I was trying to create the function in a new .m script but I am really noob at this and I can't figure out how to pass the variables.
Attached you can find my GUI program. To test it just create a file text to open. Thanks to all of you helping all of us, you are the best!

function myui
f = figure;
A = {'a.txt'};
B = {false};
myData = [A B];
t = uitable('Parent',f,...
'Position', [25 25 700 200], ...
'Data',myData,...
'ColumnEditable', [false true], ...
'CellEditCallback',@boxchecked;
% create cell files of the size of the .TRA files
files = cell(length(A),2);
function boxchecked(hObject,eventdata)
if eventdata.NewData == 1
% read the file only if it isn't already there
if isempty(files{eventdata.Indices(1),1})
fName = myData(eventdata.Indices(1),1);
fileName = fopen(fName{1,1}, 'r');
C = textscan(fileName, '%s %s %s', 'Delimiter', ';');
% create two subcells in files at the index eventdata containing the force and displacement
for i = 1:length(C{1,1})-17
files{eventdata.Indices(1),1}{i,1} = str2num(C{1,1}{i+17});
files{eventdata.Indices(1),2}{i,1} = str2num(C{1,2}{i+17});
end
end
f1 = cell2mat(files{1,1});
f2 = cell2mat(files{1,2});
plot(f1,f2)
end
end
end
2 comentarios
Adam
el 27 de Oct. de 2017
Editada: Adam
el 27 de Oct. de 2017
I would advise a different solution than nested functions with GUIDE. They are great for callbacks in programmatic UIs, but not for GUIDE. GUIDE auto-created functions do not terminate with an 'end' and adding one to every function is not a sensible solution. Any new component you add will introduce a function without an 'end' again and adding them all manually just doesn't make sense (I tried it once, but soon gave up!).
If your function myui takes no arguments then just leave it in its own file and call it from within your GUIDE file (even if it does take arguments obviously you can do the same and pass the arguments in).
When in its own file you will not have the problem of functions not being terminated by an 'end'
Stephen23
el 27 de Oct. de 2017
Editada: Stephen23
el 27 de Oct. de 2017
I would advise avoiding GUIDE altogether: writing your own code gives total control over everything, and allows using nested functions without any issues. I use nested functions for passing data between callbacks, and they really are much more convenient than any other method!
Respuesta aceptada
Jan
el 27 de Oct. de 2017
it says that some function were not closed with an end statement
This is a full description of the problem already.
- If one function in an M-file is closed with an end, all functions in this file must be close with an end.
- Nested functions must be closed by an end, otherwise it would not be clear, where it ends.
Use the auto-indentation to get a fast overview over missing end 's: Ctrl-A Ctrl-I. The orange or red marks on the right column in the editor help also to locate the problem.
7 comentarios
Jan
el 5 de Nov. de 2017
@Michael: Please post the relevant code. Otherwise I have to guess: Does you CellEditCallback start with:
function YourCellEditCallback(hObject, handles)
instead of the correct:
function YourCellEditCallback(hObject, EventData, handles)
?
Más respuestas (0)
Ver también
Categorías
Más información sobre Entering Commands en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!