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
Chun Fai Leung
Chun Fai Leung 2022년 7월 22일
I see. Thank you Sir for the extra information.

댓글을 달려면 로그인하십시오.

채택된 답변

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