I have a problem with the following try and catch code in MATLAB:
fonster='';
while ~(~isempty(fonster) && isnumeric(fonster) && isreal(fonster) && isfinite(fonster) && (fonster == fix(fonster)) && (fonster > 0))
try
fonster = input('Välj ett heltal till fönsterplatsen du vill lägga grafen i: ');
catch
disp('Du måste mata in ett heltal');
end
end
It never seems to enter the catch part of the code when entering an invalid input as a string to the input function. Instead it just runs the input all over again and ignores my output message. The wierd part is that I'm using a disp within a catch in another place in my code but this time it just doesn't work.
Here are some running examples:
Input without the try and catch statements:
Välj den fönsterplats du vill lägga grafen i: o
Error using input
Undefined function or variable 'o'.
Error in skapaPlot (line 11)
fonster = input('Välj den fönsterplats du vill lägga grafen i:
');
Error in mainMeny (line 17)
plot_handles = skapaPlot(plot_handles);
Välj den fönsterplats du vill lägga grafen i:
With try and catch:
Välj ett heltal till fönsterplatsen du vill lägga grafen i: f
Välj ett heltal till fönsterplatsen du vill lägga grafen i: d
Välj ett heltal till fönsterplatsen du vill lägga grafen i: s
Välj ett heltal till fönsterplatsen du vill lägga grafen i:
Why doesn't it display the disp part?!
Matlab's input expression evaluator will handle any exceptions raised during input, and immediately redisplay the prompt, and the exception will get cleared. So your catch handler never gets to see any exceptions. If you want to manage things yourself, use instead input(..., 's'), which returns the raw string. You can then perform your evaluation using 'eval()':
try
inputstring = input('Välj ett heltal till fönsterplatsen du vill lägga grafen i: ', 's');
fonster = eval(inputstring);
catch
disp('Du måste mata in ett heltal');
end
Related
I have a gui with nine checkbox in the following code i can select only one of each but what i wanted to add is if the NormeEm checkbox is selected i want to select another checkbox, which i tried to set up at the end of the code by using a switch and 2 cases so that only another one checkbox can be selected but it won't show and it doesn't give me an error code.
function kgexec4n(Init)
%KGEXEC4N boite de dialogue "norme" associée à kgexec4
%CAVIAR2, © ALSTOM + OL 2000/04-2003/03
error(nargchk(0,1,nargin));
persistent blkParamNorm
%%%% ouverture boite
if nargin==1
%"full block path name" du bloc ParamNorme
blkParamNorm=gcb;
if ~strcmp(get_param(blkParamNorm,'MaskType'),'ParamNor')
errordlg(kverranorm(14),'Caviar3','modal')
return
end
%création figure
hf=kgui4n;
%en cas de librairie verrouillée on interdit OK
%x=get_param(get_param(gcs,'Parent'),'Lock'); %modif2.4 10/03/2006
rep=findstr(gcs,'/');
if isempty(rep)
active_sys=gcs;
else
active_sys=gcs(1:rep-1)
end
x=get_param(active_sys,'Lock')
set( findobj(hf,'Tag','NormeOK'), 'Enable', onoff(~onoff(x)) )
else
hf=gcbf;
end
%%%% handles des contrôles et dévalidation de tout
hrb=[findobj(hf,'Tag','Norme0')
findobj(hf,'Tag','Norme1022')
findobj(hf,'Tag','NormeEm')
findobj(hf,'Tag','NormeG53')
findobj(hf,'Tag','NormeCh')
findobj(hf,'Tag','NormeUsrV')
findobj(hf,'Tag','NormeUsrI')
findobj(hf,'Tag','Norme50160')
findobj(hf,'Tag','Norme519')];
hrbVal=get(hrb,'Value');
set(hrb,'Value',0)
hno=[findobj(hf,'Tag','Param0')
findobj(hf,'Tag','Param1022')
findobj(hf,'Tag','ParamEm')
findobj(hf,'Tag','ParamG53')
findobj(hf,'Tag','ParamCh')
findobj(hf,'Tag','ParamUsrV')
findobj(hf,'Tag','ParamUsrI')
findobj(hf,'Tag','Param50160')
findobj(hf,'Tag','Param519-1')
findobj(hf,'Tag','Param519-2')
findobj(hf,'Tag','Param519-3')
findobj(hf,'Tag','Param519-4')];
set(hno,'Enable','off')
%%%% gestion dynamique de la boite
switch get(gcbo,'Tag'); %l'objet ayant provoqué l'appel
%dialogue dynamique
case {'Norme0','Norme1022'}
set(gcbo,'Value',1)
case 'NormeEm'
set(gcbo,'Value',1),enable(hno(3))
switch get(gcbo,'Tag');
case 'Norme1022'
set(findobj(hf,'Tag','Norme1022'),'Value',1)
case 'NormeUsrV'
set(findobj(hf,'Tag','NormeUsrV'),'Value',1),enable(hno(6))
end
case 'NormeG53'
set(gcbo,'Value',1),enable(hno(4))
case 'NormeCh'
set(gcbo,'Value',1),enable(hno(5))
case 'NormeUsrV'
set(gcbo,'Value',1),enable(hno(6))
case 'NormeUsrI'
set(gcbo,'Value',1),enable(hno(7))
case 'Norme50160'
set(gcbo,'Value',1),enable(hno(8))
case {'Norme519','Param519-1'}
set(hrb(9),'Value',1),Scr519(hno(9:12))
end
If i could have some help to figure out what's wrong in my code it would help me a lot
I'm parsing a text file with matlab which looks like this :
[Date]
2019-03-27 10:45:10.167618
[Component]
Component_Name : Manager principal
Component_ID : _ocl_MEu9Eemg_bhrv2HEbw
{Port}
Port_Name : FOP 1
Port_ID : _sZMXoEu9Eemg_bhrv2HEbw
Port_Type : Outgoing Port
[Component]
Component_Name : Manager 2
Component_ID : _r-HlMEu9Eemg_bhrv2HEbw
{Port}
Port_Name : FIP 1
Port_ID : _sZWIoku9Eemg_bhrv2HEbw
Port_Type : Incoming Port
[Link]
Link_Name : On/Off
Link_ID : _sZfSkku9Eemg_bhrv2HEbw
Link_Source_Name : Manager principal
Link_Source_ID : _ocl_MEu9Eemg_bhrv2HEbw
Link_Source_Port_Name : FOP 1
Link_Source_Port_ID : _sZMXoEu9Eemg_bhrv2HEbw
Link_Target_Name : Manager 2
Link_Target_ID : _r-HlMEu9Eemg_bhrv2HEbw
Link_Target_Port_Name : FIP 1
Link_Target_Port_ID : _sZWIoku9Eemg_bhrv2HEbw
I create a systeme for each [Component] i find and in each systems i create an input or output if the [Component] is a Source or a Target of a [Link].
In my text file example : On/Off is a link between 'Manager principal' and 'Manager 2'. So in the first System (Manager principal) i have an output called On/Off with a specific tag in tag description i set and in the second system (Manager 2) an input called On/Off with a specific tag in the Block tag i set.
So when i launch my code i have 2 system with 1 block in each system.
In this 2 blocks, if it's about the same link (in this example it is) we have this tag :
#_sZMXoEu9Eemg_bhrv2HEbw ==> #_sZWIoku9Eemg_bhrv2HEbw
The ID of the source port ==> the ID of the target port
This is what distinguish a Link than an other.
The context is : if the user change only the name of the Link for example Off/On instead of On/Off it must not create a new block because it's the same Link. So i would like to make a findBlocks() and for each blocks in the current system, i would like to do : if both ID's in tag description are same than in the text file so we just update the name of the block for example .
Here is the code where i create my blocks :
Update : I success to recover C which is the string inside Block tag.
B = find_system(sprintf('%s', component_NameValue));
C = get_param(find_system(sprintf('%s/%s', component_NameValue, link_NameValue)), 'Tag');
if (compareOut == 1) && (compareSource == 1) % Si c'est un port sortant et que c'est le port source du link
add_block('simulink/Ports & Subsystems/In1',[component_NameValue '/' link_NameValue]); % alors on ajoute un block Output de même nom que le link dans le système du composant que l'on parse
linkDescription = sprintf('Link : \n\n%s ==> %s \n\nComposant : \n\nDe : %s (%s) \nVers : %s (%s) \n\nPort Source : \n\n%s \n%s \n\n', link_NameValue , link_IDValue , link_SourceNameValue , link_SourceIDValue, link_TargetNameValue , link_TargetIDValue, link_SourcePortNameValue, link_SourcePortIDValue);
linkTag = sprintf('#%s ==> #%s', link_SourcePortIDValue, link_TargetPortIDValue);
set_param(gcb, 'Tag', linkTag);
set_param(gcb,'Description',linkDescription); % On ajoute une description au block contenant les infos du lien en question contenus dans le fichier texte
end
if (compareIn == 1) && (compareTarget == 1) % Si c'est un port entrant et que c'est le port target du link
add_block('simulink/Ports & Subsystems/Out1',[component_NameValue '/' link_NameValue]); % alors on ajoute un block Input de même nom que le link dans le système du composant que l'on parse
linkDescription = sprintf('Link : \n\n%s ==> %s \n\nComposant : \n\nDe : %s (%s) \nVers : %s (%s) \n\nPort Target : \n\n%s \n#%s', link_NameValue , link_IDValue , link_SourceNameValue , link_SourceIDValue, link_TargetNameValue , link_TargetIDValue,link_TargetPortNameValue, link_TargetPortIDValue);
linkTag = sprintf('#%s ==> #%s', link_SourcePortIDValue, link_TargetPortIDValue); %On ajoute un # devant l'ID du port pour pouvoir le retrouver et voir si un port à été modifié/rajouté/supprimé
set_param(gcb, 'Tag', linkTag);
set_param(gcb,'Description',linkDescription); % On ajoute une description au block contenant les infos du lien en question contenus dans le fichier texte
end
Thanks for helping in advance
ClassNN= TDBEditArpa16
I want AutoHotKey to get this value as a number and save it to an integer variable. How can I do this?
Note: I tried to do it through the following code, but the program can not identify it as a number.
ControlGetText, qtp1, TDBEditArpa16, Alteração de Produtos, Informações de Custo
StringTrimRight, qtp1, qtp1, 4
qtp1 = (%qtp1% + 2)
msgbox %qtp1%
Storing the result of an expression:
To assign a result to a variable, use the := operator.
ControlGetText, qtp1, TDBEditArpa16, Alteração de Produtos, Informações de Custo
StringTrimRight, qtp1, qtp1, 4
qtp1 := (qtp1 + 2)
msgbox %qtp1%
Variable names in an expression are not enclosed in percent signs
I wrote the following function:
function [output_signal] = AddDirectivityError (bat_loc_index, butter_deg_vector, sound_matrix)
global chirp_initial_freq ;
global chirp_end_freq;
global sampling_rate;
global num_of_mics;
global sound_signal_length;
for (i=1 : num_of_mics)
normalized_co_freq = (chirp_initial_freq + chirp_end_freq)/ (1.6* sampling_rate);
A=sound_matrix ( i, : ) ;
peak_signal=max(A);
B=find(abs(A)>peak_signal/100);
if (butter_deg_vector(i)==0)
butter_deg_vector(i)=2;
end
[num, den] = butter(butter_deg_vector(i), normalized_co_freq, 'low');// HERE!!!
filtered_signal=filter(num,den, A );
output_signal(i, :)=filtered_signal;
end
This functions runs many-many times without any error. However, when I reach the line: [num, den] = butter ( butter_deg_vector(i), normalized_co_freq, 'low');
And the local variables are: i=3, butter_deg_vector(i)=1, normalized_co_freq=5.625000e-001
MATLAB prompts an error says:
??? Error using ==> buttap Expected N to be integer-valued.
"Error in ==> buttap at 15 validateattributes(n,{'numeric'},{'scalar','integer','positive'},'buttap','N');
Error in ==> butter at 70 [z,p,k] = buttap(n);"
I don't understand why this problem occurs especially in this iteration. Why does this function prompt an error especially in this case?
Try to change the code line for:
[num, den] = butter (round(butter_deg_vector(i)), normalized_co_freq, 'low');
I'm struggling with a small program, I can't find how to correct one mistake.
My program:
program calcul
! ce programme permet d'effectuer des opérations mathématique de base
IMPLICIT NONE
REAL::x,y
character(len=1)::op
character(len=16)::op_msg
write(*,*)"entrer le type d'opération à effectuer(+,-,/,x,*)"
read(*,*)op
write(*,*)"entrer le premier nombre de l'opération"
read(*,*)x
write(*,*)"entrer le deuxième nombre de l'opération"
read(*,*)y
if(op=="+") then
write(*,*)x,"plus",y,"egale",x+y
else if(op=="-")then
write(*,*)x,"moin",y,"egale",x-y
else if ((op==("*").or.("x")) then
write(*,*)x,"multiplie par",y,"egale",x*y
else if (op=="/")then
write(*,*)x,"divise par",y,"egale",x/y
else
write(*,*)"erreur:operation incorrecte"
end if
end program calcul
The error message:
calculette.f90:21.26:
else if ((op==("*").or.("x")) then
1
Error: Invalid character in name at (1)
Any idea? I don't understand why "x" is an invalid character?
else if ((op==("*").or.op==("x")) then
You are evaluating two separate conditions, so each one needs a "left" and "right" side.