現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
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?
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!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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 (한국어)