이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
Matlab app designer confuses the end written inside matrix index with the end for if and for loop
조회 수: 2 (최근 30일)
이전 댓글 표시
farzad
2020년 5월 13일
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
farzad
2020년 5월 13일
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
2020년 5월 13일
편집: Mehmed Saad
2020년 5월 13일
is this your error?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/292695/image.png)
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
2020년 5월 13일
@farzad: please show us the complete error message. This means all of the red text.
Mehmed Saad
2020년 5월 13일
편집: Mehmed Saad
2020년 5월 13일
@farzad: you are asking a lot of questions in community and sometimes your behaviour is very rude with contributors.
farzad
2020년 5월 13일
편집: farzad
2020년 5월 13일
@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
2020년 5월 13일
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
2020년 5월 13일
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
2020년 5월 13일
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
2020년 5월 13일
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
2020년 5월 13일
편집: farzad
2020년 5월 13일
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
채택된 답변
Walter Roberson
2020년 5월 13일
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
farzad
2020년 5월 13일
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
2020년 5월 13일
when mTtot =[] ; we are in the first step of the loop, and in that step, meaning
if m==1
mTtot= t.'
end
farzad
2020년 5월 13일
am I doing a simple mistake or there is a better way to do this to not encounter this error ?
Walter Roberson
2020년 5월 14일
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
2020년 5월 14일
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
2020년 5월 14일
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
2020년 5월 18일
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
2020년 5월 18일
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
2020년 5월 18일
Thank you so much !!! that was actually the problem, I had put all the functions on the zero columns under the button
farzad
2020년 5월 20일
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- 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)
아시아 태평양
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)