Azzera filtri
Azzera filtri

plotting data that includes '-nan(incl)'

9 visualizzazioni (ultimi 30 giorni)
louis ferreira
louis ferreira il 22 Giu 2021
Commentato: Walter Roberson il 29 Giu 2021
Hi,
I have been gathering Data for experiments via .CSV files.
If the laser in the experiment is too far from a detector, the measurement at that time would, understandably, provide a 'nan'.
However, if this occurs, a function i wrote to convert .CSV data into arrays of doubles stops working, instead, it returns cells. I cannot plot these cells.
I guess the issue i am having is telling matlab to replace thoses nans with an arbitrarily large number or completely remove them and then provide me with a plottable Double array.
The Following Code contains: Csv2array function + 1 set of data plotting as expected + 1 set of data returning a cell type array rather than double:
clear all, clc, close all
%Mechanical Turbulance (Random) (Works as expected)
%T5
%Load data CSV
[T5nct, T5ncx1, T5ncy1, T5ncI1] = CSV2array("LF_BS_NC_Turb1.csv");
[T5ct, T5cx1, T5cy1, T5cI1] = CSV2array("LF_BS_C_Turb1.csv");
figure %Look at it
subplot(211)
plot(T5nct,T5ncx1, T5nct, T5ncy1, T5ct, T5cx1, T5ct, T5cy1)
legend({'NCx1(um)', 'NCy1(um)', 'Cx1(um)', 'Cy1(um)'}, 'location', "best")
title('Test 5: Random Turbulance, No Correction Vs Correction (x,y)')
xlabel('Time (seconds)');
ylabel('Distance From Centre (microns)');grid on, grid minor;
subplot(212)
plot(T5nct,T5ncI1, T5ct, T5cI1)
legend({'(NC)Intensity(Volts)', '(C)Intensity(Volts)'}, 'location', "best")
title('Test 5 (I(V))')
xlabel('Time (seconds)');
ylabel('Intensity (Volts)');
grid on, grid minor;
%T6: NC -> C in 2mins, random mechanical adjustment of FSM (Does not work
%as expected
%Load data CSV
[T6t, T6x1c, T6y1c, T6I1] = CSV2array("LF_BS_NC+CT6.csv");
%Matlab having a hissy fit, decides to convert to cell instead
T6x1=cell2num(T6x1c);
T6y1=cell2num(T6y1c);
figure %Look at it
subplot(211)
plot(T6t, T6x1, T6t, T6y1)
legend({'x1(um)', 'y1(um)'}, 'location', "best")
title('Test 6: Random Turbulance, No Correction Vs Correction (x,y)')
xlabel('Time (seconds)');
ylabel('Distance From Centre (microns)');grid on, grid minor;
subplot(212)
plot(T6t,T6I1)
legend({'(NC)Intensity(Volts)', '(C)Intensity(Volts)'}, 'location', "best")
title('Test 6 (I(V))')
xlabel('Time (seconds)');
ylabel('Intensity (Volts)');grid on, grid minor;
function [t, x, y, I] = CSV2array(CSV)
TableData = readtable(CSV); %Puts CSV into table
%separate into columns of interest
tt= TableData(:,1);
xt= TableData(:,2);
yt= TableData(:,3);
It= TableData(:,4);
%Turns each table into an array
t= table2array(tt)./1e3; %converts time to Seconds
x= table2array(xt);
y= table2array(yt);
I= table2array(It);
end
Error: Error using plot
Invalid data argument. (When trying to plot the cell array)
Blank plot when trying to use "cell2num"
I have tried many other things but these are rather convoluted in my head and i fear it would be rather lengthy and useless to try and find everything I have tried so i can list to you.
I hope everything has been layed out clearly,
Thanks

Risposta accettata

dpb
dpb il 22 Giu 2021
Use tables; you'll get benefit of variable names and easier addressing for free...the "CT6" file that your function didn't read is imported cleanly by readtable other than you may want to clean up the variable naming convention to make them allowable MATLAB variable names...
>> tCT6=readtable('LF_BS_NC+CT6.csv');
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.
>> head(tCT6)
ans =
8×11 table
ms X1_um_ Y1_um_ I1_V_ X2_V_ Y2_V_ I2_V_ RX1_V_ RY1_V_ RX2_V_ RY2_V_
__ ______ _______ _____ _____ _____ _____ ______ ______ ______ ______
0 48.262 -91.18 2.9 0.001 0 0.13 4.96 4.97 5.01 5.02
28 48.55 -91.118 2.91 0.001 0 0.13 4.96 4.97 5.01 5.02
38 48.583 -91.18 2.9 0 0 0.13 4.96 4.97 5.01 5.02
43 48.583 -91.18 2.9 0 0 0.13 4.96 4.97 5.02 5.02
53 48.583 -91.18 2.9 0.001 0 0.13 4.96 4.97 5.01 5.02
48 48.583 -91.18 2.9 0 0 0.13 4.96 4.97 5.01 5.02
74 48.583 -91.834 2.9 0 0 0.13 4.96 4.97 5.02 5.02
84 49.236 -91.18 2.9 0.001 0 0.13 4.96 4.97 5.01 5.02
>> sum(~isfinite(tCT6{:,:}))
ans =
0 93 93 0 0 0 0 0 0 0 0
>>
reads the data cleanly but shows there are the exepcted NaN elements for a couple of the variables. You can remove those for analyses; plot() will just ignore non-finite elements silently and leave gaps in the plot...
To plot, just use the variable names and cell-addressing to select the variables wanted--these names can be variables as well as constant cellstr() values shown here; that allows you to write a generic function to plot any variable or combination of variables desired simply by giving the wanted variables by name (a reason for cleaning up the naming convention some).
>> plot(tCT6{:,{'X1_um_','Y1_um_'}})
>> title('Test 6: Random Turbulance, No Correction Vs Correction (x,y)')
xlabel('Time (seconds)');
ylabel('Distance From Centre (microns)');grid on, grid minor;
>>
results in
  8 Commenti
louis ferreira
louis ferreira il 24 Giu 2021
thank you, sorry for not getting back to you quicker, the issue was resolved in 2019b by nesting a "str2num" in the plot function.
I should probably just update, this whole headache would have been avoided lol
Walter Roberson
Walter Roberson il 29 Giu 2021
Activating an older release on a license on the same host as a newer release with the same license, does not increase the activation count for the license. The activation count is per-host, per-license, regardless of which release you are using.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su MATLAB in Help Center e File Exchange

Prodotti


Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by