Problem with nested or local functions in GUIDE

5 ビュー (過去 30 日間)
Michael Daldini
Michael Daldini 2017 年 10 月 27 日
コメント済み: Jan 2017 年 11 月 5 日
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 件のコメント
Adam
Adam 2017 年 10 月 27 日
編集済み: Adam 2017 年 10 月 27 日
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
Stephen23 2017 年 10 月 27 日
編集済み: Stephen23 2017 年 10 月 27 日
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!

サインインしてコメントする。

採用された回答

Jan
Jan 2017 年 10 月 27 日
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 件のコメント
Michael Daldini
Michael Daldini 2017 年 10 月 30 日
編集済み: Michael Daldini 2017 年 10 月 30 日
Hi Jan, thank you for the answer. when I do as you say, this error pops-up:
No appropriate method, property, or field 'files' for class 'matlab.ui.eventdata.ActionData'.
Error in Test_wone_file_1>fctCB (line 110) files = handles.files;
Error while evaluating Table CellEditCallback.
What am I doing wrong?
Jan
Jan 2017 年 11 月 5 日
@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)
?

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeEntering Commands についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by