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!