open docx using popupmenu
古いコメントを表示
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 件のコメント
Rik
2020 年 4 月 14 日
Did you check the values of Z and PD(i)? It also looks like you should make all of these elseif instead. Then you can also add an else to catch unexpected values and mlint should be able to warn you about duplicate conditions.
Daniel Liberman
2020 年 4 月 14 日
Ankit
2020 年 4 月 14 日
logic is working fine! I tried to change value of Z and PD, code open different word files. I think problem is with PD(i). Are you sure you are getting different PD(i)? Can you display PD(i) values in workspace and check again?
Rik
2020 年 4 月 14 日
What are the exact values of Z and PD(i) at the time of execution? Put a breakpoint just befor the first if you showed and execute this line:
clc,fprintf('Z=%.15f PD(i)=%.15f\n',Z,PD(i))
Daniel Liberman
2020 年 4 月 15 日
Rik
2020 年 4 月 15 日
That is not the output of the line of code I posted. You think you may be withholding irrelevant information, but this is the key to your problem. If Z has a value of 2 and PD(i) a value below 30, then what you describe as a problem is the intended behavior of your code.
Daniel Liberman
2020 年 4 月 17 日
Geoff Hayes
2020 年 4 月 17 日
Daniel - how is Z being set? Please copy and paste that code to this question.
Daniel Liberman
2020 年 4 月 18 日
Daniel Liberman
2020 年 4 月 18 日
Rik
2020 年 4 月 18 日
Have you tried setting a breakpoint on the line that should set the value of Z? I don't see any indication in this code that Z is not set as it should.
You should also note that a lot of the GUIDE generated code isn't doing anything useful when it comes to explaining behavior. The last 6 functions in the code you posted could have been omitted.
Daniel Liberman
2020 年 4 月 19 日
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
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
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.
カテゴリ
ヘルプ センター および File Exchange で Variable Initialization についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
