open docx using popupmenu

2 ビュー (過去 30 日間)
Daniel Liberman
Daniel Liberman 2020 年 4 月 14 日
コメント済み: Rik 2020 年 4 月 20 日
Hi,
I am trying to open different docx files using a combination of four popupmenues in a gui and if statements.
For some reason, no matter what I choose in my popupmenus, I keep getting only the first file (10.12B.docx) opened.
Can someone tell why?
if (Z==2) && (PD(i)<30)
RN=10.12;
winopen('10.12B.docx');
end
if (Z==2) && (PD(i)>=30)
RN=10.14;
winopen('10.14B.docx');
end
if (Z==3) && (PD(i)<30)
RN=10.12;
winopen('10.12B.docx');
end
if (Z==4) && (PD(i)<30)
RN=10.14;
winopen('10.14B.docx');
end
if (Z==5) && (PD(i)<30)
RN=10.14;
winopen('10.14B.docx');
end
if (Z==6) && (PD(i)<20)
RN=10.2;
winopen('10.2C.docx');
end
if (Z==6) && (PD(i)>=20 && PD(i)<30)
RN=10.9;
winopen('10.9B.docx');
end
if (Z==6) && (PD(i)>=30)
RN=10.10;
winopen('10.10B.docx');
end
if (Z==6) && (PD(i)>=30)
RN=10.11;
winopen('10.11B.docx');
end
if (Z==7) && (PD(i)<30)
RN=10.9;
winopen('10.9B.docx');
end
if (Z==7) && (PD(i)>=30)
RN=10.10;
winopen('10.10B.docx');
end
if (Z==7) && (PD(i)>=30)
RN=10.11;
winopen('10.11B.docx');
end
if (Z==8) && (PD(i)<30)
RN=10.9;
winopen('10.9B.docx');
end
if (Z==8) && (PD(i)>=30)
RN=10.10;
winopen('10.10B.docx');
end
if (Z==8) && (PD(i)>=30)
RN=10.11;
winopen('10.11B.docx');
end
if (Z==9) && (PD(i)<30)
RN=10.9;
winopen('10.9B.docx');
end
if (Z==9) && (PD(i)>=30)
RN=10.10;
winopen('10.10B.docx');
end
if (Z==9) && (PD(i)>=30)
RN=10.11;
winopen('10.11B.docx');
end
if (Z==10) && (PD(i)<20)
RN=10.2;
winopen('10.2C.docx');
end
if (Z==10) && (PD(i)>=20 && PD(i)<30)
RN=10.9;
winopen('10.9B.docx');
end
if (Z==10) && (PD(i)>=30)
RN=10.10;
winopen('10.10B.docx');
end
if (Z==10) && (PD(i)>=30)
RN=10.11;
winopen('10.11B.docx');
end
if (Z==11) && (PD(i)<20)
RN=10.2;
winopen('10.2C.docx');
end
if (Z==11) && (PD(i)>=20 && PD(i)<30)
RN=10.9;
winopen('10.9B.docx');
end
if (Z==11) && (PD(i)>=30)
RN=10.10;
winopen('10.10B.docx');
end
if (Z==11) && (PD(i)>=30)
RN=10.11;
winopen('10.11B.docx');
end
if (Z==12) && (PD(i)<20)
RN=10.2;
winopen('10.2C.docx');
end
if (Z==12) && (PD(i)>=20 && PD(i)<30)
RN=10.9;
winopen('10.9B.docx');
end
if (Z==12) && (PD(i)>=30)
RN=10.10;
winopen('10.10B.docx');
end
if (Z==12) && (PD(i)>=30)
RN=10.11;
winopen('10.11B.docx');
end
if (Z==13) && (PD(i)<20)
RN=10.2;
winopen('10.2C.docx');
end
if (Z==13) && (PD(i)>=20 && PD(i)<30)
RN=10.9;
winopen('10.9B.docx');
end
if (Z==13) && (PD(i)>=30)
RN=10.10;
winopen('10.10B.docx');
end
if (Z==13) && (PD(i)>=30)
RN=10.11;
winopen('10.11B.docx');
end
if (Z==14) && (PD(i)<20)
RN=10.3;
winopen('10.3C.docx');
end
if (Z==14) && (PD(i)>=20 && PD(i)<30)
RN=10.4;
winopen('10.4B.docx');
end
if (Z==14) && (PD(i)>=30)
RN=10.5;
winopen('10.5B.docx');
end
if (Z==15) && (PD(i)<20)
RN=10.2;
winopen('10.2C.docx');
end
if (Z==15) && (PD(i)>=20 && PD(i)<30)
RN=10.9;
winopen('10.9B.docx');
end
if (Z==15) && (PD(i)>=30)
RN=10.10;
winopen('10.10B.docx');
end
if (Z==15) && (PD(i)>=30)
RN=10.11;
winopen('10.11B.docx');
end
if (Z==16) && (PD(i)<20)
RN=10.3;
winopen('10.2C.docx');
end
if (Z==16) && (PD(i)>=20 && PD(i)<30)
RN=10.4;
winopen('10.9B.docx');
end
if (Z==16) && (PD(i)>=30)
RN=10.7;
winopen('10.10B.docx');
end
if (Z==17)
RN=10.12;
winopen('10.12B.docx');
end
if (Z==18)
RN=10.12;
winopen('10.12B.docx');
end
if (Z==19)
RN=10.13;
winopen('10.12B.docx');
end
if (Z==20)
RN=10.12;
winopen('10.12B.docx');
end
if (Z==21)
RN=10.12;
winopen('10.12B.docx');
end
  14 件のコメント
Walter Roberson
Walter Roberson 2020 年 4 月 20 日
??? You are manipulating the content of the menus available to the popup menus dynamically, but you expect to use the relative item numbers???
I advise you to rewrite your code so that it does not rely upon menu item positions, and instead pulls the relevant string content out and compares against that. If you do that, then you can probably skip most of your nested cases, which exist mostly to be able to make sense out of which menu items are currently active.
Rik
Rik 2020 年 4 月 20 日
I would strongly suggest you use the code Walter suggested. As you can see, your current code is difficult to debug.
On a related note: are you actually using your Matlab root folder as current directory? That is a really bad idea, since you could end up editing core Matlab files, which could result in corrupting your installation. If you enjoy installing Matlab often you can keep doing this, but otherwise I suggest you use a different folder.

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

回答 (1 件)

Walter Roberson
Walter Roberson 2020 年 4 月 18 日
if (Z==6) && (PD(i)<20)
RN=10.2;
winopen('10.2C.docx');
end
if (Z==6) && (PD(i)>=20 && PD(i)<30)
RN=10.9;
winopen('10.9B.docx');
end
if (Z==6) && (PD(i)>=30)
RN=10.10;
winopen('10.10B.docx');
end
Consider:
%first value is for PD < 20, second is for 20<=PD<30, third is PD>30
RNvals = [
nan, nan, nan;
10.12, 10.12, 10.14; %Z2
10.12, 10.12, nan; %Z3
10.14, 10.14, nan; %Z4
10.14, 10.14, nan; %Z5
10.2, 10.9, 10.10; %Z6
]
Files = {'', '', '';
'12B', '12B', '14B';
'12B', '12B', '14B';
'14B', '14B', '';
'2C', '9B', '10B';
}
PDidx = (PD(:) < 20)*1 + (20 <= PD(:) & PD(:) < 30) * 2 + (PD(:) > 30) * 3;
idx = sub2ind(size(RNvals), Z, PDidx);
RN = RNvals(idx);
nanidx = find(isnan(RN));
if ~isempty(nanidx)
badZ = Z(nanidx(1));
badPD = PD(nanidx(1));
error('Z == %d is not configured for PD of %g', badZ, badPD);
end
file_selection = strcat({'10.'}, Files(idx), {'.docx'});
cellfun(@(F) winopen(F), file_selection);
Vectorized, has error checking, detects combinations not configured for.
Anywhere in your code you have long chains of if/then, you should consider configuring through arrays.
Like your image reading: you can have an array of file names according to Z values, index it by Z, and read that file.

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by