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

49 次查看(过去 30 天)
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 个评论
Vishnu priya v
Vishnu priya v 2022-7-22
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

请先登录,再进行评论。

采纳的回答

Cris LaPierre
Cris LaPierre 2022-7-21
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 个评论
Chun Fai Leung
Chun Fai Leung 2022-7-22
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Develop Apps Using App Designer 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by