Can I use a listbox as a log? - matlab

I have a listbox in my GUI on MATLAB. How can I use that listbox as a log?
My GUI is a constant loop that repeats ever minute and new data from sensors come in and I would to log whenever the temperature/humidity would go over/under optimal conditions. At the moment it only logs on the first line of the listbox and when something changes it just replaces that line with a new one and I need it to constantly make new lines so you can scroll through the log.
if Temp > MaxTemp
set(handles.listbox2,'String','Repliwax high temperature!')
end
if Temp < MinTemp
set(handles.listbox2,'String','Repliwax low temperature!')
end
if (Temp>MinTemp1) && (Temp<MaxTemp1)
set(handles.listbox2,'String','Repliwax optimal Temperature')
end

Related

How I can evaluate a condition on next time-step in anylogic?

I am trying to write a code for one state to another state transition where the system will first store the initial model time and then will check a condition (used while loop). It will continue to run the loop until the condition is false and when the condition is false it will record the final model time. So, my main objective is to get the total time that while loop condition is true. The problem is, I don't know how to check the while loop every 1 time step, For example, I tried "wait (1);" in place of "???" section of the below code which is not correct. Can anyone please suggest how I can do this?
My transition code as below:
...
...
initialTime=time();
while ((thisPed.inState(walking) && thisPed.fieldOfVision.contains(pedX, pedY));
{
???
}
finalTime= time();
exposureTime = finalTime - initialTime;
...
...
you can't put while statements in a model that at the same time run with time steps... to do that you have many other ways..
For instance you can generate a transition that goes from that state to the same state (internally) and generate your code there every time step.
Another option is to use a conditional transition in the same way
BUt NOT a while loop

How can I save output from Simulink?

I'm a student learning to use MATLAB. For an assignment, I have to create a simple state machine and collect some results. I'm used to using Verilog/Modelsim, and I'd like to collect data only when the state machine's output changes, which is not necessarily every time/sample period.
Right now I have a model that looks like this:
RequestChart ----> ResponseChart ----> Unit Delay Block --> (Back to RequestChart)
| |
------------------------> Mux --> "To Workspace" Sink Block
I've tried setting the sink block to save as "Array" format, but it only saves 51 values. I've tried setting it to "Timeseries", but it saves tons of zero values.
Can someone give me some suggestions? Like I said, MATLAB is new to me, please let me know if I need to clarify my question or provide more information.
Edit: Here's a screen capture of my model:
Generally Simulink will output a sample at every integration step. If you want to only output data when a particular event occurs -- in this case only when some data changes -- then do the following,
run the output of the state machine into a Detect Change block (from the Logic and Bit Operations library)
run that signal into the trigger port of a Triggered Subsystem.
run the output of the state machine into the data port of the Triggered Subsystem.
inside the triggered subsystem, run the data signal into a To Workspace block.
Data will only be saved at time point that the trigger occurs, i.e. when your data changes.
In your Simulink window, make sure the Relative Tolerance is small so that you can generate many more points in between your start and ending time. Click on the Simulation option at the top of the window, then click on Model Configuration Parameters.
From there, change the Relative Tolerance to something small... like 1e-10. After that, try running your simulation again. You should have a lot more points in your output array that you can now save.

Save the debug state in matlab

I am looking for a way to save 'everything' in the matlab session when it is stopped for debugging.
Example
function funmain
a=1;
if a>1
funsub(1)
end
funsub(2)
end
function funsub(c)
b = c + 1;
funsubsub(c)
end
function funsubsub(c)
c = c + 2; %Line with breakpoint
end
When I finally reach the line with the breakpoint, I can easily navigate all workspaces and see where all function calls are made.
The question
How can I preserve this situation?
When debugging nested programs that take a long time to run, I often find myself waiting for a long time to reach a breakpoint. And sometimes I just have to close matlab, or want to try some stuff and later return to this point, so therefore finding a way to store this state would be quite desirable. I work in Windows Server 2008, but would prefer a platform independant solution that does not require installation of any software.
What have I tried
1. Saving all variables in the workspace: This works sometimes, but often I will also need to navigate other workspaces
2. Saving all variables in the calling workspace: This is already better as I can run the lowest function again, but may still be insufficient. Doing this for all nested workspaces is not very convenient, and navigating the saved workspaces may be even worse.
Besides the mentioned inconveniences, this also doesn't allow me to see the exact route via which the breakpoint is reached. Therefore I hope there is a better solution!
Code structure example
The code looks a bit like this
function fmain
fsub1()
fsub2()
fsub3()
end
function fsub1
fsubsub11
fsubsub12
...
fsubsub19
end
function fsub2
fsubsub21
fsubsub22
...
fsubsub29
end
function fsub3
fsubsub31
fsubsub32
...
fsubsub39
end
function fsubsub29
fsubsubsub291
fsubsubsub292% The break may occur in here
...
fsubsubsub299
The break can of course occur anywhere, and normally I would be able to navigate the workspace and all those above it.
Checkpointing
What you're looking to implement is known as checkpointing code. This can be very useful on pieces of code that run for a very long time. Let's take a very simple example:
f=zeros(1e6,1);
for i=1:1e6
f(i) = g(i) + i*2+5; % //do some stuff with f, not important for this example
end
This would obviously take a while on most machines so it would be a pain if it ran half way, and then you had to restart. So let's add a checkpoint!
f=zeros(1e6,1);
i=1; % //start at 1
% //unless there is a previous checkpoint, in which case skip all those iterations
if exist('checkpoint.mat')==2
load('checkpoint.mat'); % //this will load f and i
end
while i<1e6+1
f(i) = g(i) + i*2+5;
i=i+1;
if mod(i,1000)==0 % //let's save our state every 1000 iterations
save('checkpoint.mat','f','i');
end
end
delete('checkpoint.mat') % //make sure to remove it when we're done!
This allows you to quit your code midway through processing without losing all of that computation time. Deciding when and how often to checkpoint is the balance between performance and lost time!
Sample Code Implementation
Your Sample code would need to be updated as follows:
function fmain
sub1done=false; % //These really wouldn't be necessary if each function returns
sub2done=false; % //something, you could just check if the return exists
sub3done=false;
if exist('checkpoint_main.mat')==2, load('checkpoint_main.mat');end
if ~sub1done
fprintf('Entering fsub1\n');
fsub1()
fprintf('Finished with fsub1\n');
sub1done=true;
save('checkpoint_main.mat');
end
if ~sub2done
fprintf('Entering fsub2\n');
fsub2()
fprintf('Finished with fsub2\n');
sub2done=true;
save('checkpoint_main.mat');
end
if ~sub3done
fprintf('Entering fsub3\n');
fsub3()
fprintf('Finished with fsub3\n');
sub3done=true;
save('checkpoint_main.mat');
end
delete('checkpoint_main.mat');
end
function fsub2
subsub21_done=false;subsub22_done=false;...subsub29_done=false;
if exist('checkpoint_fsub2')==2, load('checkpoint_fsub2');end
if ~subsub21_done
fprintf('\tEntering fsubsub21\n');
fsubsub21
fprintf('\tFinished with fsubsub21\n');
subsub21_done=true;
save('checkpoint_fsub2.mat');
end
...
if ~subsub29_done
fprintf('\tEntering fsubsub29\n');
fsubsub29
fprintf('\tFinished with fsubsub29\n');
subsub29_done=true;
save('checkpoint_fsub2.mat');
end
delete('checkpoint_fsub2.mat');
end
function fsubsub29
subsubsub291_done=false;...subsubsub299_done=false;
if exist('checkpoint_fsubsub29.mat')==2,load('checkpoint_fsubsub29.mat');end
if ~subsubsub291_done
fprintf('\t\tEntering fsubsubsub291\n');
fsubsubsub291
fprintf('\t\tFinished with fsubsubsub291\n');
subsubsub291_done=true;
save('checkpoint_fsubsub29.mat');
end
if ~subsubsub292_done
fprintf('\t\tEntering fsubsubsub292\n');
fsubsubsub292% The break may occur in here
fprintf('\t\tFinished with fsubsubsub292\n')
subsubsub292_done=true;
save(checkpoint_fsubsub29.mat');
end
delete('checkpoint_fsubsub29.mat');
end
So in this structure if you restarted the program after it was killed it would resume back to the last saved checkpoint. So for example if the program died in subsubsub291, the program would skip fsub1 altogether, just loading the result. And then it would skip subsub21 all the way down to subsub29 where it would enter subsub29. Then it would skip subsubsub291 and enter 292 where it left off, having loaded all of the variables in that workspace and in previous workspaces. So if you backed out of 292 into 29 you would have the same workspace as if the code just ran. Note that this will also print a nice tree structure as it enters and exits functions to help debug execution order.
Reference:
https://wiki.hpcc.msu.edu/pages/viewpage.action?pageId=14781653
After a bit of googling, I found that using putvar (custom function from here: http://au.mathworks.com/matlabcentral/fileexchange/27106-putvar--uigetvar ) solved this.

How to control timing of the commands (MATLAB)

I have a loop that has a command I want to execute at a specific time, say 3 seconds from now. But I don't want to add a delay because I want my rest of the statements and the loop to execute continuously without delay. Is there any way I can store statements in a buffer to execute at a required time?
You need to set up a timer object. The "Display message using Timer"-Example shows exactly what you need.
Have you tried tic and toc? Like this:
tic
executeFlag = 1;
while(1) % Your loop here.
if (toc > 3) && (executeFlag)
timedThing(); % The thing to run on a timer.
executeFlag = 0;
end
everythingElse(); % Everything else you need to do.
end
It's not pretty, but it will do the job. You can remove the executeFlag if you want the timed thing to run after a certain time.
Let me know if this doesn't do it for you, and I'll take another shot at it.

MATLAB undo a command

I am currently developing a basic image processing application on MATLAB. I have to implement undo to previous state feature. I searched net there is uiundo but it only undos GUI actions. Is there a simple command to undo. Thanks.
If you have plenty of memory to spare, you could store all of your program states in a structure and then push this structure into a circular buffer. The number of elements in the buffer would determine the number of levels of undo.
It seems to me that you would have to implement your own multi-level (or one-level) undo by using the Command pattern.
This would require you to wrap your operations into objects that contain the logic to perform the action and to restore the state. No silver bullet, hard work needed.
General undo will be complicated, but if you are only interested in saving the state of MATLAB variables and returning to a saved state, this might be one possible solution:
save_state.m:
SavedStateFolder = '/home/user/matlab_saved_states_folder/';
save([ SavedStateFolder, 'saved_state_', sprintf('%06d', (size(dir(SavedStateFolder), 1) - 1)) ]);
fprintf('state saved in saved_state_%s.mat\n', sprintf('%06d', (size(dir(SavedStateFolder), 1) - 2)));
undo_index.m:
function undo_index()
SavedStateFolder = '/home/user/matlab_saved_states_folder/';
FilesStruct = dir(SavedStateFolder);
LastSavedStateIndex = size(FilesStruct, 1) - 2;
if (LastSavedStateIndex < 1)
fprintf('No saved states available.\n');
else
fprintf('Index of last saved state is %06d\n', LastSavedStateIndex);
end
return
undo.m:
SavedStateFolder = '/home/user/matlab_saved_states_folder/';
load([ SavedStateFolder, 'saved_state_', sprintf('%06d', input('Enter saved state index ')) ]);
Then you can use save_state to save the state of MATLAB variables or you can even prefix all your commands with save_state; if you want to keep track of all changes. When you want to return to a previous state of variables, you can run undo_index to find out the index of last saved state and then you can run undo and input the index given by undo_index, or alternatively any smaller positive integer to return to an earlier state of variables. The first saved state file will be named saved_state_000001.mat, then saved_state_000002.mat and so on... Note that save_state saves everything to disk, so using SSD or RAM disk might be a good idea if you want to try this in a loop with a lot of data. Note also that the previous content of variable called SavedStateFolder is lost when you run save_state or undo. To avoid this, you can replace all instances of SavedStateFolder in save.state.m and undo.m with hard-coded folder names, for example:
save_state.m:
save([ '/home/user/matlab_saved_states_folder/', 'saved_state_', sprintf('%06d', (size(dir('/home/user/matlab_saved_states_folder/'), 1) - 1)) ]);
fprintf('state saved in saved_state_%s.mat\n', sprintf('%06d', (size(dir('/home/user/matlab_saved_states_folder/'), 1) - 2)));
undo.m:
load([ '/home/user/matlab_saved_states_folder/', 'saved_state_', sprintf('%06d', input('Enter saved state index ')) ]);
Note also that save_state, undo_index and undo assume that in SavedStateFolder there are no other files except ., .. and saved_state_*.mat files. Also the number of saved_state_*.mat files and the running index to be saved is found out this way, so if you delete some previous saved_state_*.mat files without eg. creating equal number of empty files to replace them, the counts don't match and save_state might save the state on top of an already existing saved state file.