Reading in a text file with numbers and strings in
25 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Richard Nash
am 18 Okt. 2022
Bearbeitet: Star Strider
am 18 Okt. 2022
I have a .txt which I can read into a matrix fine.
I currently do this as follows:
FILENAME = 'inputData.csv';
sondeData = readmatrix(FILENAME,'numheaderlines',1); %ignore first line as this is heading
The text file has mostly numerical values. Along with some parameters which are presented as strings.
In those catagories the data is "GREEN" or "RED" which are essentially pass / fail status/
A sample of the data is below
inputData =
GREEN GREEN 11.88 876 1332 GREEN 758 GREEN 4.006 19.08 5.09265 GREEN
GREEN GREEN 12.09 863 1519 GREEN 761 GREEN 4.006 19.08 5.070016 GREEN
When I read the file in I get NaNs in all the columns that contain the RED / GREEN.
What I want to do is clean the data up - so remove any rows where the value in a certain column is RED. This means something has failed and so data is junk.
How can I load the data in such that I can do something like the below, to remove the junk rows
inputData(inputData(:, 2)== "RED", :)= []
I am not able to change how the inputData is provided to me - but I could do an intemediary step to turn those into booleans along the lines of GREEN = 1, RED = 0 if I had to.
0 Kommentare
Akzeptierte Antwort
Star Strider
am 18 Okt. 2022
Bearbeitet: Star Strider
am 18 Okt. 2022
It would help to haved the file.
The readtable function would likely be best for this, since it will also import the variable names (likely the first line of the file).
T1 = cell2table({'GREEN' 'GREEN' 11.88 876 1332 'GREEN' 758 'GREEN' 4.006 19.08 5.09265 'GREEN';
'RED' 'RED' rand rand rand 'RED' rand 'RED' rand rand rand 'RED';
'GREEN' 'GREEN' 12.09 863 1519 'GREEN' 761 'GREEN' 4.006 19.08 5.070016 'GREEN'})
Lv = any(strcmpi(T1{:,[1 2 6 8 12]},'RED'),2) % Test Variables
T1 = T1(~Lv,:)
Experiment with the actual data.
EDIT — (18 Oct 2022 at 16:26)
Use curly braces for the table reference here —
a = inputData{1,3};
b = a^2;
.
0 Kommentare
Weitere Antworten (1)
Les Beckham
am 18 Okt. 2022
Try using readtable instead of readmatrix. This will read in the strings properly and allow you to do your cleanup based on the values of those strings.
1 Kommentar
Siehe auch
Kategorien
Mehr zu Characters and Strings finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!