I want to use the two buttons and have the text field change once a folder is selected. Then have the figures and the jpg's save to those respective folders.

1 次查看(过去 30 天)
This code is really close to done! I would like to be able to use the buttons to choose folders on where to save the formats of the plots that are created during the code. I would also like to have the text box to the right of the buttons update after a folder is selected.
classdef SEdataAPPv1_09 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
GridLayout matlab.ui.container.GridLayout
LeftPanel matlab.ui.container.Panel
FolderSelectionButtonGroup matlab.ui.container.ButtonGroup
MultipleFoldersButton matlab.ui.control.RadioButton
SingleFolderButton matlab.ui.control.RadioButton
Version109Label matlab.ui.control.Label
Image matlab.ui.control.Image
RightPanel matlab.ui.container.Panel
GridLayout2 matlab.ui.container.GridLayout
JPEGSaveLocationButton matlab.ui.control.Button
MatLabFigureSaveLocationButton matlab.ui.control.Button
JPEGSaveLocationEditField matlab.ui.control.EditField
MatLabFigureSaveLocationEditField matlab.ui.control.EditField
Lamp matlab.ui.control.Lamp
STARTButton matlab.ui.control.Button
end
% Properties that correspond to apps with auto-reflow
properties (Access = private)
onePanelWidth = 576;
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: STARTButton
function STARTButtonPushed(app, event)
% This code takes RAW SE data, turns the files into text (.txt) files and
% then combines the HIGH, MID, and LOW frequency into one text file which
% can be then multi-selected and those selected files will be put onto a
% plot.
% Enter the directory to search
parentdirectory = uigetdir('*', 'Select Folder with folders inside it');
if isnumeric(parentdirectory); return; end %user cancel
dinfo = dir(parentdirectory);
dinfo(~[dinfo.isdir]) = []; %remove ordinary files
dinfo(ismember({dinfo.name}, {'.', '..'})) = []; %remove . and ..
for K = 1 : length(dinfo)
directory = fullfile(dinfo(K).folder, dinfo(K).name);
% List all items in the folder
fileList = dir(directory);
% Delete the subfolders from the list (i.e. only keep files)
fileList(vertcat(fileList.isdir)) = [];
figure
hold on
% Uses folder as title of plot
[ParentFolderPath] = fullfile(directory);
[~, ParentFolderName] = fileparts(ParentFolderPath);
title(ParentFolderName,'FontSize',20);
% Loop through each file, copy it and give new extension: .txt
for i = 1:numel(fileList)
file = fullfile(directory, fileList(i).name);
[tempDir, tempFile] = fileparts(file);
status = copyfile(file, fullfile(tempDir, [tempFile, '.txt']));
end
% Combine HORIZONTAL text files
%location = input("location of test point: ","s");
fileName = ParentFolderName + " HORZ NOM.txt";
dL = dir(fullfile(directory,'*HLF*NOM.txt'));
dM = dir(fullfile(directory,'*HMF*NOM.txt'));
dH = dir(fullfile(directory,'*HHF*NOM.txt'));
for i = 1:numel(inf)
tD = readtable(fullfile(dL(i).folder,dL(i).name),'numheaderlines',6,'readvariablenames',1);
tD = [tD;readtable(fullfile(dM(i).folder,dM(i).name),'numheaderlines',6,'readvariablenames',1)];
tD = [tD;readtable(fullfile(dH(i).folder,dH(i).name),'numheaderlines',6,'readvariablenames',1)];
% do whatever with each set here before going on...
writetable(tD,fullfile(directory, fileName));
plot(tD.Frequency,tD.SE,"Color",'Blue',LineWidth=1.5)
hold on
end
% Combine Vertical text files
%location = input("location of test point: ","s");
fileName = ParentFolderName + " VERT NOM.txt";
dL = dir(fullfile(directory,'*VLF*NOM.txt'));
dM = dir(fullfile(directory,'*VMF*NOM.txt'));
dH = dir(fullfile(directory,'*VHF*NOM.txt'));
for i = 1:numel(inf)
tD = readtable(fullfile(dL(i).folder,dL(i).name),'numheaderlines',6,'readvariablenames',1);
tD = [tD;readtable(fullfile(dM(i).folder,dM(i).name),'numheaderlines',6,'readvariablenames',1)];
tD = [tD;readtable(fullfile(dH(i).folder,dH(i).name),'numheaderlines',6,'readvariablenames',1)];
% do whatever with each set here before going on...
writetable(tD,fullfile(directory, fileName));
plot(tD.Frequency,tD.SE,"Color",'Red',LineWidth=1.5)
hold on
end
% Combine AON text files and save as MR
%location = input("location of test point: ","s");
fileName = ParentFolderName + " AON.txt";
dL = dir(fullfile(directory,'*HLF*AON.txt')); % salt to suit wildcard to match naming convention
dM = dir(fullfile(directory,'*HMF*AON.txt'));
dH = dir(fullfile(directory,'*HHF*AON.txt'));
for i = 1:numel(inf) % there must be same number Lo, Mid, High -- add error check first
tD = readtable(fullfile(dL(i).folder,dL(i).name),'numheaderlines',6,'readvariablenames',1);
tD = [tD;readtable(fullfile(dM(i).folder,dM(i).name),'numheaderlines',6,'readvariablenames',1)];
tD = [tD;readtable(fullfile(dH(i).folder,dH(i).name),'numheaderlines',6,'readvariablenames',1)];
% do whatever with each set here before going on...
writetable(tD,fullfile(directory, fileName))
plot(tD.Frequency,tD.SE,"Color",'#588146',LineWidth=1.5)
hold on
end
% Combine FTN text files and save as DR
%location = input("location of test point: ","s");
fileName = ParentFolderName + " FTN.txt";
dL = dir(fullfile(directory,'*HLF*FTN.txt')); % salt to suit wildcard to match naming convention
dM = dir(fullfile(directory,'*HMF*FTN.txt'));
dH = dir(fullfile(directory,'*HHF*FTN.txt'));
for i = 1:numel(inf) % there must be same number Lo, Mid, High -- add error check first
tD = readtable(fullfile(dL(i).folder,dL(i).name),'numheaderlines',6,'readvariablenames',1);
tD = [tD;readtable(fullfile(dM(i).folder,dM(i).name),'numheaderlines',6,'readvariablenames',1)];
tD = [tD;readtable(fullfile(dH(i).folder,dH(i).name),'numheaderlines',6,'readvariablenames',1)];
% do whatever with each set here before going on...
writetable(tD,fullfile(directory, fileName))
plot(tD.Frequency,tD.SE,"Color",'#9f9f9f',LineWidth=1.5)
end
grid on
% Changing colors based on alphebetic order
newcolors = {'Blue','Red','#72a75b','#999999','#0003ff'};
colororder(newcolors);
% Sets pass/fail line
PFx = [10000 10000000 1000000000];
PFy = [20 80 80];
% Adds pass/fail line to plot
plot(PFx,PFy,'Color','Green','LineWidth',1.5);
% Changes legend location and removes pass/fail line from legend
Lgnd = legend('Location','northwest');
Lgnd = legend('Horizontal','Vertical','MR','DR','MIL-STD-188-125-1');
Lgnd.FontSize = 14;
% Labels both x and y axis
xlabel('Frequency (Hz)','FontSize',18,'FontWeight','bold');
ylabel('Shielding Effectivness (dB)','FontSize',18,'FontWeight','bold');
% Sets both axis scales
set(gca,'XScale','log','ylim',[-10 200]);
% Removes the values on the plot *Comment out
% set(gca,'xticklabel',[],'yticklabel',[]); to add them back in*
% set(gca,'xticklabel',[],'yticklabel',[]);
% Sets grid lines to dashes, colors
set(gca,'gridlinestyle','--');
set(gca,'gridalpha',0.5);
set(gca,'GridColor','Black');
set(gca,'Color','#ececec');
set(gca,'Color','#ececec');
set(gcf,'windowstate','maximized');
savefig(ParentFolderPath);
exportgraphics(gca,ParentFolderPath + ".jpg");
close(gcf);
end
end
% Changes arrangement of the app based on UIFigure width
function updateAppLayout(app, event)
currentFigureWidth = app.UIFigure.Position(3);
if(currentFigureWidth <= app.onePanelWidth)
% Change to a 2x1 grid
app.GridLayout.RowHeight = {313, 313};
app.GridLayout.ColumnWidth = {'1x'};
app.RightPanel.Layout.Row = 2;
app.RightPanel.Layout.Column = 1;
else
% Change to a 1x2 grid
app.GridLayout.RowHeight = {'1x'};
app.GridLayout.ColumnWidth = {220, '1x'};
app.RightPanel.Layout.Row = 1;
app.RightPanel.Layout.Column = 2;
end
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.AutoResizeChildren = 'off';
app.UIFigure.Position = [100 100 640 313];
app.UIFigure.Name = 'MATLAB App';
app.UIFigure.SizeChangedFcn = createCallbackFcn(app, @updateAppLayout, true);
% Create GridLayout
app.GridLayout = uigridlayout(app.UIFigure);
app.GridLayout.ColumnWidth = {220, '1x'};
app.GridLayout.RowHeight = {'1x'};
app.GridLayout.ColumnSpacing = 0;
app.GridLayout.RowSpacing = 0;
app.GridLayout.Padding = [0 0 0 0];
app.GridLayout.Scrollable = 'on';
% Create LeftPanel
app.LeftPanel = uipanel(app.GridLayout);
app.LeftPanel.BackgroundColor = [1 1 1];
app.LeftPanel.Layout.Row = 1;
app.LeftPanel.Layout.Column = 1;
% Create Image
app.Image = uiimage(app.LeftPanel);
app.Image.Position = [6 173 208 134];
app.Image.ImageSource = 'Jaxon_R_LOGO.png';
% Create Version109Label
app.Version109Label = uilabel(app.LeftPanel);
app.Version109Label.FontSize = 24;
app.Version109Label.Position = [40 159 139 30];
app.Version109Label.Text = 'Version 1.09';
% Create FolderSelectionButtonGroup
app.FolderSelectionButtonGroup = uibuttongroup(app.LeftPanel);
app.FolderSelectionButtonGroup.BorderType = 'none';
app.FolderSelectionButtonGroup.TitlePosition = 'centertop';
app.FolderSelectionButtonGroup.Title = 'Folder Selection';
app.FolderSelectionButtonGroup.BackgroundColor = [1 1 1];
app.FolderSelectionButtonGroup.FontSize = 24;
app.FolderSelectionButtonGroup.Position = [6 21 208 104];
% Create SingleFolderButton
app.SingleFolderButton = uiradiobutton(app.FolderSelectionButtonGroup);
app.SingleFolderButton.Text = 'Single Folder';
app.SingleFolderButton.FontSize = 18;
app.SingleFolderButton.Position = [11 36 128 31];
% Create MultipleFoldersButton
app.MultipleFoldersButton = uiradiobutton(app.FolderSelectionButtonGroup);
app.MultipleFoldersButton.Text = 'Multiple Folders';
app.MultipleFoldersButton.FontSize = 18;
app.MultipleFoldersButton.Position = [11 4 149 33];
app.MultipleFoldersButton.Value = true;
% Create RightPanel
app.RightPanel = uipanel(app.GridLayout);
app.RightPanel.BackgroundColor = [1 1 1];
app.RightPanel.Layout.Row = 1;
app.RightPanel.Layout.Column = 2;
% Create GridLayout2
app.GridLayout2 = uigridlayout(app.RightPanel);
app.GridLayout2.ColumnWidth = {'0.5x', '1x'};
app.GridLayout2.RowHeight = {'1x', '1x', 84};
app.GridLayout2.BackgroundColor = [1 1 1];
% Create STARTButton
app.STARTButton = uibutton(app.GridLayout2, 'push');
app.STARTButton.ButtonPushedFcn = createCallbackFcn(app, @STARTButtonPushed, true);
app.STARTButton.FontSize = 48;
app.STARTButton.Layout.Row = 3;
app.STARTButton.Layout.Column = 2;
app.STARTButton.Text = 'START';
% Create Lamp
app.Lamp = uilamp(app.GridLayout2);
app.Lamp.Layout.Row = 3;
app.Lamp.Layout.Column = 1;
app.Lamp.Color = [1 0 0];
% Create MatLabFigureSaveLocationEditField
app.MatLabFigureSaveLocationEditField = uieditfield(app.GridLayout2, 'text');
app.MatLabFigureSaveLocationEditField.HorizontalAlignment = 'center';
app.MatLabFigureSaveLocationEditField.FontSize = 18;
app.MatLabFigureSaveLocationEditField.FontColor = [1 0 0];
app.MatLabFigureSaveLocationEditField.Layout.Row = 1;
app.MatLabFigureSaveLocationEditField.Layout.Column = 2;
% Create JPEGSaveLocationEditField
app.JPEGSaveLocationEditField = uieditfield(app.GridLayout2, 'text');
app.JPEGSaveLocationEditField.HorizontalAlignment = 'center';
app.JPEGSaveLocationEditField.FontSize = 18;
app.JPEGSaveLocationEditField.FontColor = [1 0 0];
app.JPEGSaveLocationEditField.Layout.Row = 2;
app.JPEGSaveLocationEditField.Layout.Column = 2;
% Create MatLabFigureSaveLocationButton
app.MatLabFigureSaveLocationButton = uibutton(app.GridLayout2, 'push');
app.MatLabFigureSaveLocationButton.WordWrap = 'on';
app.MatLabFigureSaveLocationButton.FontSize = 18;
app.MatLabFigureSaveLocationButton.Layout.Row = 1;
app.MatLabFigureSaveLocationButton.Layout.Column = 1;
app.MatLabFigureSaveLocationButton.Text = 'MatLab Figure Save Location';
% Create JPEGSaveLocationButton
app.JPEGSaveLocationButton = uibutton(app.GridLayout2, 'push');
app.JPEGSaveLocationButton.WordWrap = 'on';
app.JPEGSaveLocationButton.FontSize = 18;
app.JPEGSaveLocationButton.Layout.Row = 2;
app.JPEGSaveLocationButton.Layout.Column = 1;
app.JPEGSaveLocationButton.Text = 'JPEG Save Location';
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = SEdataAPPv1_09
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end

采纳的回答

Kevin Holly
Kevin Holly 2022-12-2
Add property variables
properties
JPEGSaveLocation
MATLABFigureSaveLocation
end
add Callback to "JPEG Save Location" pushbutton
app.JPEGSaveLocation = uigetdir('*', 'Select Folder to save JPEG');
JPEGSaveLocationEditField.Value = app.JPEGSaveLocation;
add Callback to "MATLAB Figures Save Location" pushbutton
app.MATLABFigureSaveLocation = uigetdir('*', 'Select Folder to save MATLAB Figures');
MATLABFigureSaveLocationEditField.Value = app.MATLABFigureSaveLocation;

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Develop uifigure-Based Apps 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by