MATLAB: Open selected csv file and plot

csv

Hi everyone, I have excel file with many spreadsheets; I convert them into csv file. My problem I have now is I want to open the csv file using uigetfile, and plot that file. However, I don't know how to plot it since different file has different data size. Can anyone help me with that? Thanks alot.

Best Answer

Try this:
% 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 ~exist(startingFolder, 'dir')
% 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)
data = csvread(fullFileName);
x = data(:, 1); % Or wherever
y = data(:, 2);
plot(x, y, 'b*-', 'LineWidth', 2);
grid on;
title('Y vs. X', 'FontSize', 20);
xlabel('X', 'FontSize', 20);
ylabel('Y', 'FontSize', 20);