Issue with removing space from char from loaded data
이전 댓글 표시
Hello,
I am helping a colleage with some data analysis and come accross an issue I cannot find a way of fixing;
After loading in data (using textscan and loading as %s, for some reason the format of the data doesnt allow for %f, %d etc) and extracting data from the cells, it ends up in a character such as the one shown:
1 . 3 3 3
How does one remove the white space from this character in order to then properly turn it into numeric data that can then be used? I have tried things such as ~isspace yet this doesnt work; the result of isspace on the above number is
0 1 0 0 0 0 0 0 0 0 0 0 0
Which isnt useful!
If anyone knows a way around this I would be greatfully appriciative! Or indeed ideas on why %d, %f etc doesnt work in the textscan, as that might also solve the problem...
Many thanks David
댓글 수: 4
Stephen23
2017년 11월 3일
@David: please edit your question and upload a sample file by clicking the paperclip button.
David
2017년 11월 3일
Stephen23
2017년 11월 3일
Why are you using %s for loading numeric data? Why not simply load numeric data using a numeric format and get a numeric variable?
David
2017년 11월 3일
채택된 답변
추가 답변 (1개)
KL
2017년 11월 3일
0 개 추천
if you're only importing numeric data from those files, why not just use dlmread or csvread or textread. I'd personally prefer readtable.
It'S better to import the data clearly than having to deal with the problem of improper imports.
check these links:
댓글 수: 4
What do you mean by the size of columns keeps changing? I see perfectly tab spaced data. About ignoring the headerline, if you read any of the links I gave you, you would have seen that you could simply ignore the first line (if you want to). For example,
filename = 'somename.txt';
delimter = ' ';
rowstoIgnore = 1;
columnstoIgnore = 0;
data = dlmread(filename,delimiter, rowstoIgnore, columnstoIgnore);
on the other hand, if you use readtable, it understands X and Y are variable names and stores them as well. For example,
data = readtable(filename);
now you can access the data like,
data.X %or data.Y
David
2017년 11월 3일
Stephen23
2017년 11월 3일
"...and things like dlmread find issues with this, at least to my knowledge"
This does not mean you should jump straight to creating some complex work-around using strings. You could have asked about how to import the numeric data first. Only if that proved really difficult should you start to investigate other methods.
카테고리
도움말 센터 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!