Problem getting Matlab to plot data

2 visualizaciones (últimos 30 días)
Lee Lewis
Lee Lewis el 16 de Jun. de 2022
Comentada: Star Strider el 16 de Jun. de 2022
The problem I'm running into is that that I can't plot that data. The error I keep getting is "error using plot, invalid data argument." I don't know if I'm getting this error because there are blank cells in the data (i tried to get rid of this using line 36) or becuse I wasn't able to turn the data into doubles (like on line 29). The reason why this code seems like overkill is because the idea behind the program is that it will be able to read different files generated from another piece of software. That's why I am trying to generate a for loop that indexs all of the columns automatically. The number of columns could change from csv file to csv file. Also a lot of the columns will have blank cells, which unfortunetly makes the task a bit more difficult. Thank you in advance for the help!
%recieve file
filename = 'dataLeeProject.csv';
%modify with opts to get rid of NaN
opts = detectImportOptions(filename);
opts = setvartype(opts, 'char');
%read the table
data = readtable(filename,opts);
%get the number of rows and columns
numRows = height(data);
numCols = width(data);
%extracting Header titles for describing the frequency that the radios are
%Transmitting and recieving at.
titleTx = data{1,1};
titleTxValue=data{1,2};
titleHz=data{1,3};
titleRx=data{2,1};
titleRxValue=data{2,2};
titleHz2=data{2,3};
%generate figure
f1 = figure(1);
cla; hold on; grid on;
xAxisTitleFreq = data{3,1};
xAxisDataFreq = cellfun(@str2num,data{4:end,1});
%for loop for generating data under headers i.e(frequency, EMI, etc.)
for i=2:numCols
%Extract data from readData
yAxisDataPwr = data{4:end,i};
%have plot ignore blank cells
yAxisDataPwr{~cellfun(@isempty, yAxisDataPwr)};
%Plot data
plot(xAxisDataFreq, yAxisDataPwr);
end
  1 comentario
Walter Roberson
Walter Roberson el 16 de Jun. de 2022
I suggest using readmatrix() which will fill the empty locations with nan.
In the case where you have a completely empty column (or detected as such, so empty for the first few hundred rows at least) then readtable tends to make the variable a cell array instead of numeric. That can be changed by setvartype() but readmatrix is easier

Iniciar sesión para comentar.

Respuesta aceptada

Star Strider
Star Strider el 16 de Jun. de 2022
Add:
yAxisDataPwr = str2double(data{4:end,i});
to convert the character vectors to numerical values just before the plot call. Character arrays do not plot.
%recieve file
filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/1034725/dataLeeProject.csv';
%modify with opts to get rid of NaN
opts = detectImportOptions(filename);
opts = setvartype(opts, 'char');
%read the table
data = readtable(filename,opts);
%get the number of rows and columns
numRows = height(data);
numCols = width(data);
%extracting Header titles for describing the frequency that the radios are
%Transmitting and recieving at.
titleTx = data{1,1};
titleTxValue=data{1,2};
titleHz=data{1,3};
titleRx=data{2,1};
titleRxValue=data{2,2};
titleHz2=data{2,3};
%generate figure
f1 = figure(1);
cla; hold on; grid on;
xAxisTitleFreq = data{3,1};
xAxisDataFreq = cellfun(@str2num,data{4:end,1});
%for loop for generating data under headers i.e(frequency, EMI, etc.)
for i=2:numCols
%Extract data from readData
yAxisDataPwr = data{4:end,i};
%have plot ignore blank cells
yAxisDataPwr{~cellfun(@isempty, yAxisDataPwr)};
yAxisDataPwr = str2double(data{4:end,i});
%Plot data
plot(xAxisDataFreq, yAxisDataPwr);
end
It might be best to import the file as numeric at the outset, however you may have a specific reason for doing it this way.
.
  2 comentarios
Lee Lewis
Lee Lewis el 16 de Jun. de 2022
Thank you so much that worked. Still very new to Matlab I appreciate the help!
Star Strider
Star Strider el 16 de Jun. de 2022
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Line Plots en Help Center y File Exchange.

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by