Reading in a text file with numbers and strings in
25 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Star Strider
2022-10-18
编辑:Star Strider
2022-10-18
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 个评论
更多回答(1 个)
Les Beckham
2022-10-18
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!