Showing posts with label Matlab-HowTo. Show all posts
Showing posts with label Matlab-HowTo. Show all posts

Wednesday, January 6, 2016

Create Simple GUI in Matlab

It's always easier if you can put your Matlab coding in GUI. Especially if you have lot of functions, you can use different push-buttons for each functions. This post will guide you to create a simple GUI, consists of your program title, push-button for function, axes to display your simulation/results.

Steps:

1. In Matlab Command Window
>> guide
choose 'Blank  GUI (default)
2. A window will pop-out, you can design your GUI anyway you like. I usually keep it simple. There are 3 basic things you need for a basic GUI:

  • Static text - optional but I like to have title for all my programs (double-click to access the properties)
  • Push button - I used 2 push buttons, for 'Run' and 'Stop' (double-click to access the properties)
  • Axes - this is where your simulation shown. You can set your Axes to have toolbar
Some note on Axes: I usually have toolbar for Axes, so I can navigate to each pixels and get their information, or zoom in and out. 
To customise your Axes toolbar:
  1. Single left-click on Axes 
  2. Click 'Tools' on the menu
  3. Select 'Toolbar Editor'
  4. Drag the following to the top: Pan, zoom in, zoom out, data cursor (you can customise to your liking)
Building Matlab simple GUI

3. Rearrange and resize your window GUI accordingly. Save your GUI.

4. Once you save your gui (.fig file) an m-file will be automatically generated (the .fig and .m file will have the same name)

5. Open the GUI's m-file, and copy-paste each m-files function to each designated buttons. For instance, the 'Run' button is  under function pushbutton1_callback(hObject, eventdata, handles). I can put all my code under this button.

The 'Stop' button is under function pushbutton2_Callback(hObject, eventdata, handles). I already set my 'Run' button to execute a while loop (eg: while stop==0), so I can set my 'Stop' button as following.

push-button 'Stop'

Basic GUI

Friday, December 4, 2015

Adding xlwrite to Matlab Static Path

Disclaimer: I'm not sure if the following steps going to mess up your system. This is what works for me, atm. Use at your own risk.

In my earlier post, I added the xlwrite and its needed 6 jar files to Matlab dynamic path. However every time I restart Matlab, I have have to re-do this setting again. I hate going back to my own post and checking out the steps, spending another 1 min to set up the system to use xlwrite. I know 1 min is nothing, but I like things automated. To go about this, add the jar files to Matlab static path. Check here for more about Static Path vs Dynamic Path.

I look everywhere on how to add jar files to static path. I didn't find any clear instructions on how to edit the classpath.txt, anyway this is what works for me.

Steps:

1. In Matlab command window:
>> edit classpath.txt
2. M-file will open. MATLAB reads the static class path only at startup. You can see these are all the files loaded at startup. Eg:
$matlabroot/java/jar/zh_CN/web_connectivity_res.jar

3. We want Matlab to load our desired jar files at startup too. So put your jar files (poi_library) in matlabroot/java/jar/. Where on earth is this place? I'm using Mac, so that place is
Applications> Matlab>right-click>Show Package Contents>Java>jar

matlabroot/java/jar

4. Back to Matlab, append the following at classpath.txt
$matlabroot/java/jar/poi_library/poi-3.8-20120326.jar
$matlabroot/java/jar/poi_library/poi-ooxml-3.8-20120326.jar
$matlabroot/java/jar/poi_library/poi-ooxml-schemas-3.8-20120326.jar
$matlabroot/java/jar/poi_library/xmlbeans-2.3.0.jar
$matlabroot/java/jar/poi_library/dom4j-1.6.1.jar
$matlabroot/java/jar/poi_library/stax-api-1.0.1.jar

Append classpath.txt
 5. Save and restart Matlab.


Note: If you have warning regarding 'undefined xlwrite function', means you haven't set the path to xlwrite yet. To set the path:


Monday, November 30, 2015

Warning: Could not start Excel server for export - Use xlwrite

Update 04/12/15 - The following steps will add xlwrite and its jar files to Matlab dynamic path. However, you will have to redo the steps again every time you restart Matlab. To go about this, add the jar files to Matlab static path. Check here.

I can use xlsread in Matlab for Mac successfully, however using xlswrite gives me the following error:


After some googling and trying on csvwrite, I find another code on Mathworks. I strongly suggest those out there that hesitate to use csvwrite to give this a try.

This pic-post will walk you through on how to use xlwrite.

Step 1:
1. Download xlwrite .

2. Unzip it and put all the files in your designated folder. (I'm a digital-organised freak).

Content of your downloaded files.

3. We want to know the path to all this POI files. On Mac, click cmd+i.
cmd+i to get the information of the files


4. In Matlab command windows, use javaaddpath to add all the needed jar files.

Or you can add 'xlwrite' to Matlab path. 




That's all. You can see 'test_xlwrite.xlsx' is created in your current folder.

See file xlwrite or go to <url> for full license details.

Wednesday, January 14, 2015

Export m-file as pdf in Matlab for Mac

First of all, the interface in Matlab for Mac could be different from Windows.

Step 1: Select Edit Publishing Option
Step 1


Step 2: Select the output file as 'pdf'
Step 3: Choose your output folder
Step 2 & 3




Monday, December 8, 2014

Adding multiple ports to scope in Matlab Simulink

1. Click the scope
2. Select setting
3. On the general tab, change the number of axes









You may also use the 'mux' block to display multiple inputs.

Tuesday, December 2, 2014

Editing Text in GUI-Matlab

On command windows,
>> guide(charGUI2.fig)

can edit the text by:

  1. double click on text
  2. Look for 'String' and click the 'left alignment'




Thursday, October 9, 2014

Extract Video Frame (non-avi) in Matlab

The original author for this code can send me an email so that I can credit back to him/her.
Work for both avi and non-avi file

clc;
close all;

%% USE THIS IF - open a non-avi file
mov = mmreader('n3.mp4')
frame = read(mov,200); %LONG WAIT
mov.NumberOfFrames;

%% or USE THIS IF - Open an avi file
%filename = 'n3.wmv';
%mov = mmreader(filename);

%% continue here
% Output folder

outputFolder = fullfile(cd, 'frames');
if ~exist(outputFolder, 'dir')
mkdir(outputFolder);
end

%getting no of frames

numberOfFrames = mov.NumberOfFrames;
numberOfFramesWritten = 0;
for frame = 1 : numberOfFrames

thisFrame = read(mov, frame);
outputBaseFileName = sprintf('%3.3d.png', frame);
outputFullFileName = fullfile(outputFolder, outputBaseFileName);
imwrite(thisFrame, outputFullFileName, 'png');
progressIndication = sprintf('Wrote frame %4d of %d.', frame,numberOfFrames);
disp(progressIndication);
numberOfFramesWritten = numberOfFramesWritten + 1;
end
progressIndication = sprintf('Wrote %d frames to folder "%s"',numberOfFramesWritten, outputFolder);

disp(progressIndication);