Clear Filters
Clear Filters

Importing file in App designer of MATLAB and use the file afterwards

60 views (last 30 days)
Hi all. Thank you for helping. Lately, I have written some code in MATLAB but I need to put those MATLAB code into the app designer.
The following are the original code from MATLAB.
videoFReader = vision.VideoFileReader('12.35mW-cm22.mjpeg.avi');
videoFWriter = vision.VideoFileWriter('12.35mW-cm22_gray.mjpeg.avi',...
'FrameRate',videoFReader.info.VideoFrameRate);
while ~isDone(videoFReader)
videoFrame = step(videoFReader);
step(videoFWriter, rgb2gray(videoFrame));
end
release(videoFReader);
release(videoFWriter);
I constructed a Filename EditField and A Browse button. In the app designer code view, I used the following code and it worked.
% Button pushed function: BrowseButton
function BrowseButtonPushed(app, event)
[file,path] = uigetfile({'*.avi'},'Please select the particle tracking video');
app.FilenameEditField.Value = fullfile(file);
figure(app.UIFigure);
end
However, for the Transform to grayscale Button, I don't know how to use the imported files. I tried using "file" and "filename" etc. but it doesn't work. I tried something like this.
% Button pushed function: TransformtograyscaleButton
function TransformtograyscaleButtonPushed(app, event)
videoFReader = vision.VideoFileReader('file');
videoFWriter = vision.VideoFileWriter('file_gray',...
'FrameRate',videoFReader.info.VideoFrameRate);
while ~isDone(videoFReader)
videoFrame = step(videoFReader);
step(videoFWriter, rgb2gray(videoFrame));
end
release(videoFReader);
release(videoFWriter);
end
I want to import a video, and after transform to the grayscale video, it should generate a grayscale video with the "original file name"_gray. Could anyone help? I am new to both MATLAB and app designer, searched online for the importing file part already but could not find anything like this. I really appreciate your help.
  2 Comments
Vishnu priya v
Vishnu priya v on 22 Jul 2022
In normal matlab file, if a data is assigned to a variable, you can use it throughout the file until you change its data. But it's not the case in app designer. You need to add "app." before variable name, for it to be used throughout the code. Because if you add "app." it will be saved in app data memory, otherwise it will be limited to that particular callback section only

Sign in to comment.

Accepted Answer

Cris LaPierre
Cris LaPierre on 21 Jul 2022
Use app properties to share data within your app. See this page:
Instead of 'file', use the property value. For example, create a property to capture the source video file.
properties (Access = public)
vidFileIn % Description
end
Then assign a value to your property.
% Button pushed function: BrowseButton
function BrowseButtonPushed(app, event)
[file,path] = uigetfile({'*.avi'},'Please select the particle tracking video');
app.vidFileIn = fullfile(file);
app.FilenameEditField.Value = app.vidFileIn
figure(app.UIFigure);
end
Finally, use your app property again in your transformation function.
% Button pushed function: TransformtograyscaleButton
function TransformtograyscaleButtonPushed(app, event)
videoFReader = vision.VideoFileReader(app.vidFileIn);
videoFWriter = vision.VideoFileWriter('file_gray',...
'FrameRate',videoFReader.info.VideoFrameRate);
while ~isDone(videoFReader)
videoFrame = step(videoFReader);
step(videoFWriter, rgb2gray(videoFrame));
end
release(videoFReader);
release(videoFWriter);
end
  1 Comment
Chun Fai Leung
Chun Fai Leung on 22 Jul 2022
Thank you Sir. It worked really well. Other than vidFileIn, I also used the properties as inserting and sharing excel data as well. Thank you so much.

Sign in to comment.

More Answers (0)

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!