Clear Filters
Clear Filters

Matlab GUI to select and plot data from csv file.

19 views (last 30 days)
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

Anjaneyulu Bairi
Anjaneyulu Bairi on 12 Jan 2024
Edited: Anjaneyulu Bairi on 12 Jan 2024
Hi,
I understand that you are trying write a code for a matlab GUI app which has the option to the user to 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.You can try the below troubleshooting steps to resolve the errors.
  • The error "Unrecognized function or variable 'mainFig'." says 'mainFig' is unrecognized. The object 'mainFig' is getting created after the function call 'resizeCallback', which is calling 'setPositions' and this function has reference to the 'mainFig' object which is not created yet.
  • To resolve that error create the 'mainFig' without the function callback like below.
mainFig = figure('Name', 'CSV Plotter', 'NumberTitle', 'off', 'Position', [100, 100, 800, 400]);
  • Now set the callback property to 'mainFig' using set function like below and it will remove all the errors in the code.
set(mainFig,"ResizeFcn",@resizeCallback);
Here is the complete code for reference.
function csvPlotterGUI
% Create the main figure
mainFig = figure('Name', 'CSV Plotter', 'NumberTitle', 'off', 'Position', [100, 100, 800, 400]); % create the main figure object without function call
set(mainFig,"ResizeFcn",@resizeCallback); % now add function call
% 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
I hope it helps to resolve your query.
  1 Comment
Zakaria
Zakaria on 14 Jan 2024
Thank you for the feedback. By the time you posted yiu answer I have solved the problem.
Thank you again for you help.
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

Sign in to comment.

More Answers (2)

Image Analyst
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');

Voss
Voss on 12 Jan 2024
Edited: Voss on 12 Jan 2024
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 Labels and Annotations in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!