Getting rid of goto()-messes due to inconsistencies

1 view (last 30 days)
[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
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
case 2 %No
goto ('Fitting') %% Line 117 This breaks when pressed at all, no matter if there're vectors defined already or not
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;
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
%%%------------------- below is a part of code later on.
%% Section 2: Fitting
% LABEL Fitting %% Line 117
[FitindxGeneral,tf1]=listdlg('ListString',FitListGeneral,'PromptString','Select Degree','Name','PolyX','SelectionMode','single','ListSize',[160 130])
switch FitindxGeneral
case 1
[Fitindx, tfx2]=listdlg('ListString',Fitlist,'PromptString','Select Fittype','Name','Fittype-Selection','SelectionMode','Single','ListSize',[160 130])
switch Fitindx
case 1
clear NumCols
for k=1:1:NumRows
hold on
hold on
disp('For the fits of the different functions, open the "Fit"-cell')
case 2
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
Claudius Simon Appel
Claudius Simon Appel on 17 Mar 2020
Thank you John for a sorta kick in the butt to get me to throw out the goto-crap.

Sign in to comment.

Accepted Answer

Steven Lord
Steven Lord on 17 Mar 2020
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
done = askUserIfTheyWantToReset;
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.

More Answers (0)


Find more on Environment and Settings 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!