Getting rid of goto()-messes due to inconsistencies

1 回表示 (過去 30 日間)
Claudius Simon Appel
Claudius Simon Appel 2020 年 3 月 17 日
Hello,
[This is a rather long introduction to the problem, the beginning of the question is marked as QUESTION]
first things first, I am relatively new to matlab. For the past three days, I have been working on a script that let's me define and plot the fits of two data sets against each other from another file. The idea is to just call the script, tick all check boxes that you want to have ticked and let it go through with plotting the figure.
It is intended to limit user-input to decision and vector-definition, without having to write the actual code for
0) A menu to jump to skip certain steps and jump elsewhere
1) Defining the vectors
2) Fitting them
- all standard fits from matlab will be included, and if I get it to work, an option for custom definition of fits
3) Plotting the fits
4) Defining axes, title etc of the plot (not yet included)
Now, till now I´ve been using the goto()-script that has been uploaded by Husam Aldahiyat to the File Exchange. And it worked for ~70% of the time. It is used to navigate and skip certain parts in cases. I've included the unmodified version of it that is used. In addition, here's the link to the Exchange-page
It is used to skip the Definition and/or Fitting Parts, for instance if a typo in an axis description has been made. This would normally force one to call the script again, which defeats its purpose a bit.
It is possible to run the script once successfully (till where it is right now, at least). But if I try to jump to certain sections, goto either drops me somewhere else or gives me an error, that at least one END is missing inside the goto()-code. I can't find it, although I may just
QUESTION
As I am aware, goto can create a huge mess, which is why you all would scream at me not to use it and use functions instead. I tried wrapping my head around them, but for some reason I can't figure out how to do it with those. I tried to find a way to navigate as I am doing right now, but can't get these functions to work. What I basically need is a function that I can call that skips to any point in the script that is labeled as LABEL xyz.
I thought of just creating one huge switch-case-system and just type every possible version out. That would certainly be possible, but I don't think it'd be neither the most efficient nor enjoyable mean of doing this. Not to mention that that would amass to a shitton of code to sift through if something DOES go wrong. By now, I am fed up with this. I shortly meddled with the idea pitched by Jan as an answer to another persons goto-question (cf. Accepted Answer Script), but I don't know how to get it to work for what I have in mind.
To try and get the first step explained, here is a mindmap that shows where everything is supposed to lead.
Bottom Line is, I don't know nor understand the matlab functions well enough ~or at all~ to code this network, and goto() breaks down.
I`d be glad if someone could help me with this.
Resetlist={'Repeat','No','Yes','Jump to:'};
[resetindx,tf0]=listdlg('ListString',Resetlist,'PromptString','Reset?','Name','Reset?','SelectionMode','single','ListSize',[160 60]);
switch resetindx
case 1 %Repeat
goto ('RepeatReset') %%goes to Line 38
return
case 2 %No
goto ('Fitting') %% Line 117 This breaks when pressed at all, no matter if there're vectors defined already or not
return
case 3 %Yes %% just runs till Line 68
clear all; %has to be renamed into clearing all Variables of this script, so that the original one stays intact.
close all;
clc
case 4
SectionofInterest={'Section 1: Vector Definition','Section 2: Fitting','Section 3: Graphic Settings'};
[jumpindx,tf0]=listdlg('ListString',SectionofInterest,'PromptString','Go where?','Name','Go where?','SelectionMode','single','ListSize',[160 50]);
switch jumpindx
case 1 %% just runs till Line 68
case 2
goto('Fitting') %% LIne 117
return
end
end
%%%------------------- below is a part of code later on.
%% Section 2: Fitting
% LABEL Fitting %% Line 117
FitListGeneral={'PolyX','ExponentialX','FourierX','GaußianX','PowerX','RationalX_X','Weibull','InterpolantX','Custom_not_working'};
[FitindxGeneral,tf1]=listdlg('ListString',FitListGeneral,'PromptString','Select Degree','Name','PolyX','SelectionMode','single','ListSize',[160 130])
switch FitindxGeneral
case 1
Fitlist={'Poly1','Poly2','Poly3','Poly4','Poly5','Poly6','Poly7','Poly8','Poly9'};
[Fitindx, tfx2]=listdlg('ListString',Fitlist,'PromptString','Select Fittype','Name','Fittype-Selection','SelectionMode','Single','ListSize',[160 130])
switch Fitindx
case 1
[NumRows,NumCols]=size(maty)
clear NumCols
for k=1:1:NumRows
hold on
Fit{k}=fit(matx(k,:)',maty(k,:)','poly1')
plot(matx(k,:),maty(k,:),'o')
plot(Fit{1,k},'')
hold on
pause(1)
end
disp('For the fits of the different functions, open the "Fit"-cell')
case 2
[NumRows,NumCols]=size(maty)
clear NumCols
%%-- And below is the error-message you get when trying to say "No" to resetting, and then choose polyX --> poly1
Error using goto (line 638)
Error: At least one END is missing: the statement may begin here.
Error in Plotter_v04_Testbranch_Question (line 47)
goto ('Fitting') %% Line 117 This breaks when pressed at all, no matter if there're vectors defined already or not
  4 件のコメント
John D'Errico
John D'Errico 2020 年 3 月 17 日
編集済み: John D'Errico 2020 年 3 月 17 日
I recall a colleague who long ago was my mentor as I began work. He used fortran as his language of choice, as did all of us at the time.
He had one huge deck of cards that he used to solve all of his programming problems. Yes, we used card decks at the time - large boxes of cards read into a card reader. Our fortran code was compiled and executed on the mainframe.
Once, he needed to ballance his checkbook. (NO LIE HERE.) So his solution was to append a block of cards at the end of his code. Then at the beginning, he inserted a branch to go to that end block, where all he did was to add and subtract some numbers. The entire mess of code was compiled and executed to balance his checkbook. The next time he wanted to use that code for something else, he removed the goto, or he would change the initial branch to some other line. But the checkbook balance part was probably left untouched. After all, he might want to balance his checkbook again next month.
This is spaghetti code at the ultimate. It is also why I rapidly learned not to write code as he did.
Claudius Simon Appel
Claudius Simon Appel 2020 年 3 月 17 日
Thank you John for a sorta kick in the butt to get me to throw out the goto-crap.

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

採用された回答

Steven Lord
Steven Lord 2020 年 3 月 17 日
Your mindmap has your problem broken up into small chunks. That's the first step towards modularizing your code. As you correctly guessed I would make each of those chunks either scripts or functions, which would make your main script or function look something like:
while ~done
getDataFromUser;
startFitting;
fitAllData;
plotAllGraphs;
setAxesRangesNamesEtc;
done = askUserIfTheyWantToReset;
end
Each of these pieces, if written as a script, could use all the data from the other pieces. But since they probably don't need all the data produced by the other pieces, I would design them as functions that accept only what they need to do their jobs and return only their "finished products." That compartmentalization of work means that ideally once you write one of these functions, if you need to change its internal implementation that function is the only thing you need to modify and if you need to change its inputs or outputs only those functions upstream that create or manipulate the inputs or only those functions downstream that use the outputs need to be changed. It also means they can be tested in isolation from one another, which for a complicated system can be a Very Good Thing.
This approach, particularly if you use functions, will require (or at least benefit if you do) some up-front design work. You've already done part of that, as I mentioned, with your mindmap. But I've written MATLAB code in the past that simulated goto (as part of investigating some old Fortran code) and it required some mental gymnastics and some whiteboard scribbling to keep straight what was going on and where I was going in which handbasket :) I strongly encourage you to go the script or function route.

その他の回答 (0 件)

カテゴリ

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

タグ

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by