You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Matlab app designer confuses the end written inside matrix index with the end for if and for loop
2 views (last 30 days)
Show older comments
Hi all
I got a piece of my code that is causing function nesting error and the root cause is that matlab is not understanding the end written in Matrix(end)
is not related to the loop or function end and has messed up
exactly where I haveTtot(end) , this end is confused
for fn=1:numel(files)
ff= xlsread(strcat(files{fn},'.xlsx'));
filename=files{fn};
t = ff(:,1);
resol=t(2)-t(1);
motion6t=ff;
mulnames=readcell('Ms.xlsx')
mask = strcmp(filename, mulnames(:,1));
multip = cell2mat(mulnames(mask, 2));
resol=t(2)-t(1)
if fn==1
motiontot=[];
for m=1:multip
if m==1
Ttot=t.';
else
Ttot= [Ttot,t.'+Ttot(end)+resol];
end
end
else
for m=1:multip
Ttot= [Ttot,t.'+Ttot(end)+resol];
end
end
for m=1:multip
motiontot=[motiontot;motion6t];
end
end
11 Comments
farzad
on 13 May 2020
What error would you get if you misplace an end expression??
Matlab will take the tail of that misplacement to the next end and the error will depend on the number of loops and functions. I think the focus of the question is confusing the end inside the paranthesis. When I comment that line, everything is fine
Mehmed Saad
on 13 May 2020
Edited: Mehmed Saad
on 13 May 2020
is this your error?
What is the value of Ttot when your loop run for the first time? because if f1 is not equal to 1, the else condition will run.
Stephen23
on 13 May 2020
@farzad: please show us the complete error message. This means all of the red text.
Mehmed Saad
on 13 May 2020
Edited: Mehmed Saad
on 13 May 2020
@farzad: you are asking a lot of questions in community and sometimes your behaviour is very rude with contributors.
farzad
on 13 May 2020
Edited: farzad
on 13 May 2020
@Mehmed: First I thank you for your contribution. But I don't understand your rant. This is the first time I have such a complaint here while have not even felt where the problem is. I may have asked a lot of questions but my interactions with you as far as I remember has not been in all of my questions, so it remains a question for me, why before letting me answer your question, you jumped to judge my behavior , where have I been very rude ( and not even just rude ) ? that' a very strange judgement. I might be frustrated with matlab error not you, by the way.
and sorry , it had not to be f1 , but fn, but still the same problem
When I hover on the word end in Tot(end) , Matlab should not match that end with a previous if or for expression. it just shouldn't. I get some sort of the error message you mention but in the grey part of app designer code. only due to the misreasing of end expression, the next chain of end words assigned to irrelevant loops and this causes the pushbutton end get lost and the code give me some error like
Illegal use of reserved keyword "methods".
which is in the grey part of app designer
Walter Roberson
on 13 May 2020
The error you indicate does not occur in the section of code you posted. If you were to attach the full code, we might be able to figure out what is going on.
farzad
on 13 May 2020
The full code is really long dear Walter. and I couldn't share it. I was assuming this error is known or has some known root cause. I am checking al the paranethsis and intdentations under the push button
Walter Roberson
on 13 May 2020
The typical root cause for these kinds of messages is a missing ' or " or ]
Double-check that you do not accidentally have a ‘ (right quote, U+2018) instead of a ' (apostrphone, U+0027)
farzad
on 13 May 2020
Dear Walter, I checked, and it seems that whenever I remove this block , the whole code is ok. the only suspicious ' sign, was t.' in the summation equation with Ttot. but when I removed it, I still have the same error. I had some variables with the warning : the variable spans among more functions. I resolved it as well, but still I have the same problem :
for fn=1:numel(files)
ff= xlsread(strcat(files{fn},'.xlsx'));
fname=files{fn};
t = ff(:,1);
resol=t(2)-t(1);
motion6t=ff;
cd(myperc)
mulnames=readcell('Multipliers.xlsx');
mask = strcmp(fname, mulnames(:,1));
mu = cell2mat(mulnames(mask, 2));
resol=t(2)-t(1);
if fn==1
motiontot=[];
for m=1:mu
if m==1
mTtot=t;
else
mTtot= [mTtot ,t.'+mTtot(end)+resol];
end
end
else
for m=1:mu
mTtot= [mTtot ,t.'+mTtot(end)+resol];
end
end
for m=1:mu
motiontot=[motiontot;motion6t];
end
farzad
on 13 May 2020
Edited: farzad
on 13 May 2020
Ok this is the complete code, and trying this on workspace I get error on mTtot:
Array indices must be positive integers or logical values.
Error in test (line 47)
mTtot= [mTtot ,t.'+mTtot(end)+resol];
mydir=uigetdir()
content = dir('*.xlsx');
content([content.isdir]) = []; % remove directories
files = cell(size(content));
% Extract the filename without the extentions
for fi = 1:numel(files)
[~, files{fi}] = fileparts(content(fi).name);
end
for fn=1:numel(files)
ff= xlsread(strcat(files{fn},'.xlsx'));
fname=files{fn};
t = ff(:,1);
resol=t(2)-t(1);
motion6t=ff;
mulnames=readcell('Ms.xlsx');
mask = strcmp(fname, ulnames(:,1));
mu = cell2mat(mulnames(mask, 2));
mTtot=[];
resol=t(2)-t(1);
if fn==1
motiontot=[];
for m=1:mu
if m==1
mTtot=t;
else
mTtot= [mTtot ,t.'+mTtot(end)+resol];
end
end
else
for m=1:mu
mTtot= [mTtot ,t.'+mTtot(end)+resol];
end
end
for m=1:mu
motiontot=[motiontot;motion6t];
end
maxlim=max(max(motion6t(:,2:7)));
minlim=min(min(motion6t(:,2:7)));
directory=pwd;
end
Accepted Answer
Walter Roberson
on 13 May 2020
mTtot=[]
size(mTtot)
mTtot =
[]
ans =
0 0
with it being empty, end as an index would be 0. mTtot(end) would be mTtot(0) which is not permitted.
mTtot= [mTtot ,t.'+mTtot(end)+resol];
Perhaps you are wanting mTtot(end) to be empty instead of an error when mTtot is empty. But if that were the case, then you would have the situation
mTtot = [[], t.'+[]+resol]
and anything + [] is [], so you would be dealing with
mTtot = [[], []]
which would leave mTtot empy, but I suspect you are trying to append to it in that statement,.
16 Comments
farzad
on 13 May 2020
Thank you very much for the answer Walter. well my aim is to append the time series one after the other. this was working somewhere else in my code, but since my code has become super complex, I tried to understand it here.
so in the first step , you have the first time series , let's say 35 seconds with 0.01 seconds of resolution, if the second time series is 15 seconds, by appending , mTtot should be 35+15 = 50 seconds but to append it to an array, I need the above algorithm, maybe it needs imrpovements
farzad
on 13 May 2020
when mTtot =[] ; we are in the first step of the loop, and in that step, meaning
if m==1
mTtot= t.'
end
farzad
on 13 May 2020
am I doing a simple mistake or there is a better way to do this to not encounter this error ?
Walter Roberson
on 14 May 2020
Yes, but suppose that fn is not 1 when mTtot is empty. You go to the else part, and in that else part you do not have the if m==1 protection.
farzad
on 14 May 2020
you are right ! I tried that as bellow, and I got :
Error :
Error using horzcat
Dimensions of arrays being concatenated are not consistent.
Error in test (line 50)
mTtot= [mTtot ,t.'+mTtot(end)+resol];
if fn==1
motiontot=[];
for m=1:mu
if m==1
mTtot=t;
else
mTtot= [mTtot ,t.'+mTtot(end)+resol];
end
end
else
for m=1:mu
if m==1
mTtot=t;
else
mTtot= [mTtot ,t.'+mTtot(end)+resol];
end
end
end
Walter Roberson
on 14 May 2020
if fn==1
motiontot=[];
for m=1:mu
if m==1
mTtot = t(:).';
else
mTtot= [mTtot, t(:).'+mTtot(end)+resol];
end
end
else
for m=1:mu
if m==1
mTtot = t(:).';
else
mTtot= [mTtot, t(:).'+mTtot(end)+resol];
end
end
end
farzad
on 18 May 2020
dear Walter, after going ahead with my code, and adding other lines, not relevant to this loop, I mean : they should not affect it at all , now I see that Matlab does not pass through this loop and does not generate mTtot at all. that confusion of end expressions still exists though, but I hope it's just a visualization problem of app designer. the loop works fine itself in matlab base, but now I don't knw how to resolve it
Walter Roberson
on 18 May 2020
Perhaps you could use the editor "Smart Indent" feature on your code; by following the indentation levels you might be able to figure out why it thinks you have an extra end statement.
farzad
on 18 May 2020
Thank you so much !!! that was actually the problem, I had put all the functions on the zero columns under the button
farzad
on 20 May 2020
dear Walter, I wanted to avoid using end in brackets. So I tried
if fn==1
motiontot=[];
for m=1:mu
if m==1
Ttot = t(:).';
Tsize=size(Ttot)
else
Ttot= [Ttot, t(:).'+Ttot(Tsize)+resol];
Tsize=size(Ttot)
end
end
else
for m=1:mu
if m==1
Ttot = t(:).'; % I am not if sure it should be Ttot= [Ttot, t(:).'+Ttot(Tsize)+resol]; ?
Tsize=size(Ttot)
else
Ttot= [Ttot, t(:).'+Ttot(Tsize)+resol];
Tsize=size(Ttot)
end
end
end
I have a doubt, that when fn > 1 , I think Ttot should be already assigned with some values. but in matlab I get error. more than that, if the total number of fn is bigger than 2 I get a dimension mismatch problem
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements 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!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)