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
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
2022년 7월 21일
Use app properties to share data within your app. See this page:
https://www.mathworks.com/help/matlab/creating_guis/share-data-across-callbacks-in-app-designer.html
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Develop Apps Using App Designer에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!