Menu inside menu in Matlab - matlab

I have created a menu with different options using order 'menu'. The problem is, that I want to click one of that options and make another menu show in screen with another set of options. How can I make this kind of nested menu structure?
My code:
q=menu ('What point?:','opt1','opt2');
switch q
case 'opt1'
q1=menu('What subpoint?:','opt11','opt12');
switch q1
case 'opt11'
case 'opt12'
end
case 'opt2'
q2=menu('What subpoint?:','opt21','opt22');
switch q2
case 'opt21'
case 'opt22'
end
end

Your code is fine, except that the returned choice by menu is numeric, not the option strings. So you should use case 1 rather than case 'opt1'.
A good practice for switch is to include otherwise block, like
switch q
case 1
% do opt1
case 2
% do opt2
otherwise
disp(q)
error('Invalid option')
end
Then you will know it goes to otherwise block due to some error in your case.

Related

Access 2007 form: event AFTER undo

I have a form in Access 2007, which has an "update" routine, that enables or disables certain textboxes based on values in other fields (textboxes, checkboxes, comboboxes). The regular operation of that routine works well.
Now I found that pressing ESC calls the undo function, that restores the original values in all fields. But this undo does not call the events on those fields, so the form is in a wrong state, where textboxes are disabled/enabled although they shouldn't.
I also found that there is an undo-event, but that is useless for me because it is called before undo. I need an event after undo. What can I do here to update the fields when ESC is pressed?
I like this solution more, because it works not only on the "ESC"-Key:
private Sub form_Undo(cancel as integer)
afterUndo = true
TimerInterval = 1
end Sub
private Sub Form_Timer()
if afterUndo then
'do something after the Undo-Event
end if
TimerInterval = 0
end Sub
Well, like many times before I have an idea for a solution after postion the question.
The solution here is enabling KeyPreview on the form and using the KeyUp event. The undo is called on KeyDown, so when KeyUp is raised, the form already has the restored values again and the update routine works.
Another solution to this problem is to use each control's OldValue property. Through experimentation, I've found that the three different value properties of controls come into play in different situations:
Control.Value or simply Control
The control's current value most of the time
When the control has focus, it is the value the control had before gaining focus
During a Form.Undo event, it is the value the control prior to the Undo
Relevant during the Control.AfterUpdate and Control.Undo events
Control.Text
The control's value while it has focus
Relevant during events that occur as the user types, such as Control.Change, Control.KeyUp, Control.KeyDown
Control.OldValue
The value the control had when the current record was first opened
Also the value that a form-level Undo will reset the control to
Relevant during the Form.Undo event
The timer-based answer to this question is my go-to solution, but if you're already handling events as the user types (e.g., for live validation), then code like this can be sensible:
Private Sub LastName_Change()
ValidateLastName SourceProperty:="Text"
End Sub
Private Sub LastName_Undo(Cancel As Integer)
ValidateLastName SourceProperty:="Value"
End Sub
Private Sub Form_Undo(Cancel As Integer)
ValidateLastName SourceProperty:="OldValue"
End Sub
Private Sub ValidateLastName(SourceProperty As Variant)
Dim LastName As String
Select Case SourceProperty
Case "LastName"
LastName = Nz(Me.LastName.Text, "")
Case "Value"
LastName = Nz(Me.LastName.Value, "")
Case "OldValue"
LastName = Nz(Me.LastName.OldValue, "")
Case Else
Debug.Print "Invalid case in ValidateLastName"
Exit Sub
End Select
' <Do something to validate LastName>
End Sub
Note that this method does not get you access to the post-Form-Undo value of Control.Column(x) of combo/list boxes. You can get it by using DLOOKUP on Control.OldValue, but you're probably better off just using the timer-based solution instead.

Using a Position Function in a Case Function - PostgreSQL

I'm new to SQL and Postgres, so hopefully this isn't too hard to figure out for all of you.
I'm trying to use a Position function within a CASE statement, but I keep getting the error
"ERROR: Syntax error at or near ""Project"". LINE 2: CASE WHEN position('(' IN "Project") >0 THEN".
I've used this position function before and it worked fine, so I'm confused what the problem is here. I've also tried the table name, such as "xyztable.Project" and "Project" - both without quotation marks.
Here is the entire statement:
SELECT "Project",
CASE WHEN postion('(' IN "Project") >0 THEN
substring("Project",position('(' IN "Project")+1,position(')' IN "Project")-2)
CASE WHEN postion('('IN "Project") IS NULL THEN
"Project"
END
FROM "2015Budget";
As I haven't gotten to past the second line of this statement, if anyone sees anything that would prevent this statement from running correctly, please feel free to point it out.
New Statement:
SELECT "Project",
CASE
WHEN position('(' IN "Project") >0 THEN
substring("Project",position('(' IN "Project")+1,position(')' IN "Project")-2)
WHEN position('('IN "Project") IS NULL THEN
"Project"
END
FROM "2015Budget";
Thank you for your help!!
The error is due to a simple typo - postion instead of position.
You would generally get a much more comprehensible error message in situations like this (e.g. "function postion(text,text) does not exist"). However, the use of function-specific keywords as argument separators (as mandated by the SQL standard) makes this case much more difficult for the parser to cope with.
After fixing this, you'll run into another error. Note that the general form of a multi-branch CASE expression is:
CASE
WHEN <condition1> THEN <value1>
WHEN <condition2> THEN <value2>
...
END

Specman e : Conditional Constraint in config sequence

In the config sequence I would like to create a conditional constraint. I have two clock rates and only a set of combinations of the clock rates are legal in the design.
I am having trouble coming up with the correct syntax to achieve this.The use of case or if statements give me a syntax error. This is what I have right now.
case main_clk{
1e6:{
keep div_clk == select{
80: 5e9;
20: 5.6e9;
};
};
.
.
.
};
I also tried using when but it didn't work. Any suggestions on how I can implement this?
First of all, I see from you using scientific notation from your numbers that you are using real fields. You should probably switch to using uint fields as they are more flexible from a generation point of view.
The case statement is procedural and cannot be used for generation. The same goes for if. To generate a field conditionally on the other you need to use the => (implication) operator:
keep main_clk == 1e6 =>
soft div_clk == select {
80: 5e9;
20: 5.6e9;
};
You need one such implication for each possible value of main_clk.

Matlab popupmenus

I'm writing a Gui in matlab but I have a problem with popups.
This is the code I wrote:
str=get(handles.popupmenu1, 'String');
val=get(handles.popupmenu1, 'Value');
switch str{val}
case 1
val=Normal;
case 2
val=t;
end
handles.val = val;
guidata(handles.figure1, handles);
I want to use the string in the popup in another function as its input.
But matlab selects both the values, Normal and t, so that the other function (garchfit) does not work. How can I tell Matlab to get just the String the user selects?
Thank you
You are redefining val in a strange way here. You probably want to change the assignments to val in the switch statement to some other variable. Depending on what doing on here, you might not need that switch statement at all. The selected string from the popup menu is str{val}. You can use that as the input to garchfit.
garchfit(str{val})

what's this matlab code doing?

I'm fairly new to matlab and wrote this following code:
datadir=('/.../prod/balanceSheet/DB/');
seriesnames = {'a.m','b.m','c.m','d.m','f.m','g.m','h.m','i.m'};
for proj=1:5;
database='';
switch proj
case 1
database=strcat(datadir,'scenario1');
case 2
database=strcat(datadir,'scenario2');
case 3
database=strcat(datadir,'scenario3');
case 4
database=strcat(datadir,'scenario4');
case 5
database=strcat(datadir,'scenario5');
end;
database;
gooddatanames={};
a=length(seriesnames);
for i=1:a
gooddatanames={gooddatanames,database,seriesnames(i)};
end
end
this is my first time using a switch. basically what I'm trying to do is to take series from databases (1,2,3,...) such that all series are subject to all scenarios. I'm missing the function that would pull the data but is the above code doing the intended?
Change:
gooddatanames={gooddatanames,database,seriesnames(i)};
to
gooddatanames={gooddatanames{:},database,seriesnames{i}};
and move gooddatanames = {} outside of the loop, and then it does what I think you expect, which is to produce a 1x80 cell array with alternating folders and file names.
More likely, make a few more changes, like this:
datadir=('/.../prod/balanceSheet/DB/');
seriesnames = {'a.m','b.m','c.m','d.m','f.m','g.m','h.m','i.m'};
gooddatanames={};
for proj=1:5;
database='';
switch proj
case 1
database=fullfile(datadir,'scenario1');
case 2
database=fullfile(datadir,'scenario2');
case 3
database=fullfile(datadir,'scenario3');
case 4
database=fullfile(datadir,'scenario4');
case 5
database=fullfile(datadir,'scenario5');
end;
for i=1:length(seriesnames);
gooddatanames{end+1} = fullfile(database,seriesnames{i});
end
end
which results in a 1x40 array of full paths to the individual files.
I agree with what Pursuit has written, though I would like to add that your for/switch structure is a little silly. If you effectively have to enumerate all of them, as you do with the 'switch' as you've implemented it, there's no reason not to take the for/switch loops out entirely and just leave yourself with the commands. One possible alternative would be to replace the entire unnecessary "switch" with:
database = fullfile(datadir, ['scenario', num2str(proj)]);