How to use the same directory for all functions in app designer?

I have created a GUI, in that when I add the data from a folder it will load .csv and text files. Now I wanted to add images(.png) lauanch with image viewer from the same directory in app designer.
(Here I have enclosed the example file. i.e It will ask for the select file from the directory after that it launches windows media player with the file.)
But for GUI, without using the 'baseFileName = uigetfile('*.png')' , Is it possible to select from the directory? by using 'uigetfile' it is again asking to select the folder.
for example : it should be like this baseFileName = '*.png'

1 commentaire

I do not understand, what you are asking for. Do you want to process all PNGs fils inside a specific folder?
Folder = 'D:\Yor\Folder';
FileList = dir(fullfile(Folder, '*.png'));
for k = 1:numel(FileList)
aFile = fullfile(FileList(k).folder, FileList(k).name);
% Now do what you need with this file
end

Connectez-vous pour commenter.

 Réponse acceptée

This works on my computer:
Folder = 'E:\Movies';
% FileList = dir(fullfile(Folder, '*.mkv'));
FileList = dir(fullfile(Folder, '*.wmv'))
% Not sure WMP plays .mkv files???? Does it?
if isempty(FileList)
errorMessage = sprintf('Error: no video files found in\n%s.', Folder);
fprintf('%s\n', errorMessage)
uiwait(warndlg(errorMessage));
return;
else
fprintf('Found %d videos in "%s".\n', length(FileList), Folder);
end
% Play all videos one after the other.
for k = 1:numel(FileList)
fullFileName = fullfile(FileList(k).folder, FileList(k).name);
% First construct the command line for the system() function.
% Enclose all filenames in double quotes because we may have spaces in the filenames.
arguments = sprintf('"%s"', fullFileName);
commandLine = sprintf('"%s" %s', editorFullFileName, arguments);
fprintf('%s', commandLine);
system(commandLine);
promptMessage = sprintf('Do you want to play the next video,\nor Quit processing?');
titleBarCaption = 'Continue?';
buttonText = questdlg(promptMessage, titleBarCaption, 'Continue', 'Quit', 'Continue');
if contains(buttonText, 'Quit', 'IgnoreCase', true)
return; % or break or continue.
end
end

10 commentaires

Thank you, now its working. In this can we add desired video file i.e it should go to the folder and from that can we select one video to play instead of play all videos at a time?
Sure. Just get rid of the for loop and use this snippet to ask the user to locate the one video they want:
% Have user browse for a file, from a specified "starting folder."
% For convenience in browsing, set a starting folder from which to browse.
startingFolder = pwd; % or 'C:\wherever';
if ~isfolder(startingFolder)
% If that folder doesn't exist, just start in the current folder.
startingFolder = pwd;
end
% Get the name of the file that the user wants to use.
defaultFileName = fullfile(startingFolder, '*.*');
[baseFileName, folder] = uigetfile(defaultFileName, 'Select a file');
if baseFileName == 0
% User clicked the Cancel button.
return;
end
fullFileName = fullfile(folder, baseFileName)
It's working. But in the GUI when I pushed the button it is opening a windows mediaplayer once. But it should be like when I click the button it should open another mediaplayer to select other file. Is it possible to create that?
i.e when I click the button it should execute again eventhough the previous windowsmediaplayer opened.
I tried adding a & after the command line and that let me choose a new video however it played in the same instance of the media player as the last one.
% Initialization Steps.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
folder = 'D:\My Videos';
% FileList = dir(fullfile(Folder, '*.mkv'));
FileList = dir(fullfile(folder, '*.wmv'))
% Not sure WMP plays .mkv files???? Does it?
editorFullFileName = 'C:\Program Files\Windows Media Player\wmplayer.exe';
if ~isfile(editorFullFileName)
errorMessage = sprintf('Cannot find the Windows Media Player program.\n%s', editorFullFileName);
uiwait(warndlg(errorMessage));
return;
end
if isempty(FileList)
errorMessage = sprintf('Error: no video files found in\n%s.', folder);
fprintf('%s\n', errorMessage)
uiwait(warndlg(errorMessage));
return;
else
fprintf('Found %d videos in "%s".\n', length(FileList), folder);
end
% Have user browse for a file, from a specified "starting folder."
% For convenience in browsing, set a starting folder from which to browse.
startingFolder = folder; % or 'C:\wherever';
if ~isfolder(startingFolder)
% If that folder doesn't exist, just start in the current folder.
startingFolder = pwd;
end
% Get the name of the file that the user wants to use.
defaultFileName = fullfile(startingFolder, '*.*');
[baseFileName, folder] = uigetfile(defaultFileName, 'Select a file');
if baseFileName == 0
% User clicked the Cancel button.
return;
end
fullFileName = fullfile(folder, baseFileName)
% First construct the command line for the system() function.
% Enclose all filenames in double quotes because we may have spaces in the filenames.
arguments = sprintf('"%s"', fullFileName);
commandLine = sprintf('"%s" %s &', editorFullFileName, arguments);
fprintf('%s', commandLine);
% Play the video.
system(commandLine);
Some programs are like that, where they only allow one instance at a time. You might try with different video players to see if they allow multiple instances. Even if I double clicked or selected multiple videos from File Explorer, it only lets me have one instance running at the same time.
Lavanya
Lavanya le 27 Juin 2022
Modifié(e) : Lavanya le 27 Juin 2022
No, what I am asking is -
When I click the button from GUI it asks to select a file and then it launches windows mediaplayer with the selected file.
But when I click the same button again it should do the above function again. (i.e.It should ask select a file something like that
Image Analyst
Image Analyst le 27 Juin 2022
Modifié(e) : Image Analyst le 27 Juin 2022
It does/will/should. If you're now using a GUI with buttons intead of a simple m-file script, then attach your .mlapp file (if using App Designer), or your .fig and .m file (if using GUIDE).
Lavanya
Lavanya le 28 Juin 2022
Modifié(e) : Lavanya le 28 Juin 2022
Ok, I got the solution for this. I have another question regarding the time delay
I have created the GUI in Matlab 2018b version and for the new features like Currentpoint and datacursormode I have been using Matlab 2021b version. Because of this version change I am experiencing few issues.
For example : I have used a Slider value changing to see the image slices.But when left the slider scroll it takes time to update the slice images. Is there any solution for this? kindly anyone can help?
I don't know. Try using tic and toc or timeit to find out where all the time is being spent. Or maybe it's waiting until the CPU is available to update things. Try putting in some drawnow in there to force an immediate update.
Ok, thank you . I will try these
Lavanya
Lavanya le 4 Juil 2022
Modifié(e) : Lavanya le 5 Juil 2022
I have create two apps. 1.GUI
2. Listboxedit
From GUI button click, it will open a new window with the Listbox data( it will open 2nd app ListBox edit). Till now Its working good.
But I wanted to activate the app2. When I click moveup it should change the Listorder by moving upward button. In Editfield, it should show Listbox name. Finally I wanted to save all these details and it should update data in Main GUI.
I have enclosed the image and code should work like this
For your reference I have enclosed the ListBox edit. kindly suggest a way for this?

Connectez-vous pour commenter.

Plus de réponses (2)

Try this:
folder = pwd; % wherever you want
% Get a list of all .txt, .png, and .csv files:
fileList = [...
dir(fullfile(folder, '*.txt'));...
dir(fullfile(folder, '*.png'));...
dir(fullfile(folder, '*.csv'))]
% If you want all the names put one cell array:
allFileNames = fullfile({fileList.folder}, {fileList.name})'
% Now files are listed in the order you called dir().
% If you want the list sorted alphabetically:
allFileNames = sort(allFileNames)
Kevin Holly
Kevin Holly le 24 Juin 2022
You could have the app load the directory files into a dropdown box after selecting a particular file as shown in the example attached.

2 commentaires

I am unable to open this file. Can you please help me with this?
What version of MATLAB do you have? I created the above in R2022a.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Startup and Shutdown dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by