Matlab GUI to select and plot data from csv file.
Show older comments
Hello community,
I am trying to write a code for a matlab GUI that let the user browser and select a .csv data file, then reads and shows the data, then select variables for x-axis and y-axis and finally plots the selected data.
the code runs well till I click plot botton it shows the error below.
Can anyone help please.
The code
function csvPlotterGUI
% Create the main figure
mainFig = figure('Name', 'CSV Plotter', 'NumberTitle', 'off', 'Position', [100, 100, 800, 400], 'ResizeFcn', @resizeCallback);
% Create UI components
fileButton = uicontrol('Style', 'pushbutton', 'String', 'Select CSV File', 'Callback', @selectFileCallback);
fileText = uicontrol('Style', 'text', 'String', 'Selected File:');
fileDisplay = uicontrol('Style', 'text');
xText = uicontrol('Style', 'text', 'String', 'Select X Variable:');
xPopup = uicontrol('Style', 'popupmenu');
yText = uicontrol('Style', 'text', 'String', 'Select Y Variable:');
yPopup = uicontrol('Style', 'popupmenu');
plotButton = uicontrol('Style', 'pushbutton', 'String', 'Plot', 'Callback', @plotCallback);
% Data display table
dataTable = uitable('ColumnName', {}, 'ColumnWidth', {175});
% Variables to store data
selectedFile = '';
data = table();
% Set initial positions
setPositions();
% Callback functions
function setPositions()
figPosition = get(mainFig, 'Position');
buttonWidth = 120;
buttonHeight = 30;
textWidth = 200;
textHeight = 20;
% Update positions based on the figure size
set(fileButton, 'Position', [20, figPosition(4) - 50, buttonWidth, buttonHeight]);
set(fileText, 'Position', [150, figPosition(4) - 45, textWidth, textHeight]);
set(fileDisplay, 'Position', [350, figPosition(4) - 45, textWidth, textHeight]);
set(xText, 'Position', [20, figPosition(4) - 100, textWidth, textHeight]);
set(xPopup, 'Position', [150, figPosition(4) - 100, buttonWidth, buttonHeight]);
set(yText, 'Position', [20, figPosition(4) - 150, textWidth, textHeight]);
set(yPopup, 'Position', [150, figPosition(4) - 150, buttonWidth, buttonHeight]);
set(plotButton, 'Position', [20, figPosition(4) - 200, buttonWidth, buttonHeight]);
set(dataTable, 'Position', [500, 50, figPosition(3) - 520, figPosition(4) - 100]);
end
function resizeCallback(~, ~)
setPositions();
end
function selectFileCallback(~, ~)
[filename, path] = uigetfile('*.csv', 'Select CSV File');
if filename ~= 0
selectedFile = fullfile(path, filename);
set(fileDisplay, 'String', selectedFile);
data = readtable(selectedFile);
variables = data.Properties.VariableNames;
set(xPopup, 'String', variables);
set(yPopup, 'String', variables);
updateDataTable();
end
end
function updateDataTable()
if isempty(data)
return;
end
% Display data in the uitable
set(dataTable, 'Data', table2cell(data), 'ColumnName', data.Properties.VariableNames);
end
function plotCallback(~, ~)
if isempty(selectedFile) || strcmp(selectedFile, 'Selected File:')
errordlg('Please select a CSV file first.', 'Error');
return;
end
xVarIndex = get(xPopup, 'Value');
yVarIndex = get(yPopup, 'Value');
xData = data.(data.Properties.VariableNames{xVarIndex});
yData = data.(data.Properties.VariableNames{yVarIndex});
figure;
plot(xData, yData, 'o-');
xlabel(data.Properties.VariableNames{xVarIndex});
ylabel(data.Properties.VariableNames{yVarIndex});
title('CSV Data Plot');
end
end
The error
Unrecognized function or variable 'mainFig'.
Error in untitled/setPositions (line 30)
figPosition = get(mainFig, 'Position');
Error in untitled/resizeCallback (line 53)
setPositions();
Error using untitled
Error while evaluating Figure SizeChangedFcn.
You might see this error if any of the variables in the figure's SizeChangedFcn are undefined when the figure becomes visible. Consider waiting to set the figure's Visible property to 'on' until all the SizeChangedFcn variables are defined.
Warning: 'popupmenu' control requires a non-empty Character vector
Control will not be rendered until all of its parameter values are valid
Warning: 'popupmenu' control requires a non-empty Character vector
Control will not be rendered until all of its parameter values are valid
Warning: Column headers from the file were modified to make them valid MATLAB identifiers
before creating variable names for the table. The original column headers are saved in the
VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable
names.
Error using plot
Invalid data argument.
Error in untitled/plotCallback (line 91)
plot(xData, yData, 'o-');
Error while evaluating UIControl Callback.
Accepted Answer
More Answers (2)
Image Analyst
on 12 Jan 2024
Edited: Image Analyst
on 14 Jan 2024
Use the current figure, gcf:
figPosition = get(gcf, 'Position');
that has all the wiring for selecting a folder and listing files in a listbox that the user can click on and display the plot.
OK you can just pass in mainFig to the functions that need it, like:
% Set initial positions in main program by calling the nested function setPositions().
setPositions(mainFig);
% Callback functions
function setPositions(mainFig)
figPosition = get(mainFig, 'Position');
Notice what the error message tells you: "You might see this error if any of the variables in the figure's SizeChangedFcn are undefined when the figure becomes visible. Consider waiting to set the figure's Visible property to 'on' until all the SizeChangedFcn variables are defined."
Following that advice, i.e., making the figure invisible when it's created and making it visible only after mainFig and all the other variables are defined, e.g.:
function csvPlotterGUI
% Create the main figure
mainFig = figure( ...
'Name', 'CSV Plotter', ...
'NumberTitle', 'off', ...
'Position', [100, 100, 800, 400], ...
'ResizeFcn', @resizeCallback, ...
'Visible','off');
% ... your code to create UI components here ...
set(mainFig,'Visible','on');
% Callback functions
% ... the rest of your code here ...
end
should resolve the error.
Note that you do not need to explicitly call setPositions() when initializing because that is (effectively) your figure's ResizeFcn, so it gets called when the figure is made visible.
Complete code for reference:
function csvPlotterGUI
% Create the main figure
mainFig = figure('Name', 'CSV Plotter', 'NumberTitle', 'off', 'Position', [100, 100, 800, 400],'ResizeFcn', @resizeCallback,'Visible','off');
% Create UI components
fileButton = uicontrol('Style', 'pushbutton', 'String', 'Select CSV File', 'Callback', @selectFileCallback);
fileText = uicontrol('Style', 'text', 'String', 'Selected File:');
fileDisplay = uicontrol('Style', 'text');
xText = uicontrol('Style', 'text', 'String', 'Select X Variable:');
xPopup = uicontrol('Style', 'popupmenu');
yText = uicontrol('Style', 'text', 'String', 'Select Y Variable:');
yPopup = uicontrol('Style', 'popupmenu');
plotButton = uicontrol('Style', 'pushbutton', 'String', 'Plot', 'Callback', @plotCallback);
% Data display table
dataTable = uitable('ColumnName', {}, 'ColumnWidth', {175});
% Variables to store data
selectedFile = '';
data = table();
set(mainFig,'Visible','on')
% Callback functions
function setPositions()
figPosition = get(mainFig, 'Position');
buttonWidth = 120;
buttonHeight = 30;
textWidth = 200;
textHeight = 20;
% Update positions based on the figure size
set(fileButton, 'Position', [20, figPosition(4) - 50, buttonWidth, buttonHeight]);
set(fileText, 'Position', [150, figPosition(4) - 45, textWidth, textHeight]);
set(fileDisplay, 'Position', [350, figPosition(4) - 45, textWidth, textHeight]);
set(xText, 'Position', [20, figPosition(4) - 100, textWidth, textHeight]);
set(xPopup, 'Position', [150, figPosition(4) - 100, buttonWidth, buttonHeight]);
set(yText, 'Position', [20, figPosition(4) - 150, textWidth, textHeight]);
set(yPopup, 'Position', [150, figPosition(4) - 150, buttonWidth, buttonHeight]);
set(plotButton, 'Position', [20, figPosition(4) - 200, buttonWidth, buttonHeight]);
set(dataTable, 'Position', [500, 50, figPosition(3) - 520, figPosition(4) - 100]);
end
function resizeCallback(~, ~)
setPositions();
end
function selectFileCallback(~, ~)
[filename, path] = uigetfile('*.csv', 'Select CSV File');
if filename ~= 0
selectedFile = fullfile(path, filename);
set(fileDisplay, 'String', selectedFile);
data = readtable(selectedFile);
variables = data.Properties.VariableNames;
set(xPopup, 'String', variables);
set(yPopup, 'String', variables);
updateDataTable();
end
end
function updateDataTable()
if isempty(data)
return;
end
% Display data in the uitable
set(dataTable, 'Data', table2cell(data), 'ColumnName', data.Properties.VariableNames);
end
function plotCallback(~, ~)
if isempty(selectedFile) || strcmp(selectedFile, 'Selected File:')
errordlg('Please select a CSV file first.', 'Error');
return;
end
xVarIndex = get(xPopup, 'Value');
yVarIndex = get(yPopup, 'Value');
xData = data.(data.Properties.VariableNames{xVarIndex});
yData = data.(data.Properties.VariableNames{yVarIndex});
figure;
plot(xData, yData, 'o-');
xlabel(data.Properties.VariableNames{xVarIndex});
ylabel(data.Properties.VariableNames{yVarIndex});
title('CSV Data Plot');
end
end
Categories
Find more on App Building in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!