Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Writing to CSV file help

조회 수: 2 (최근 30일)
Rodriguez Pham
Rodriguez Pham 2015년 9월 16일
마감: MATLAB Answer Bot 2021년 8월 20일
So I have a CSV file with a list of variables with various assignments, so for example: *note these are each in on cell each
a = 5464,,,,,,,
b = 657578,0.46454,0,85976
c = 46,,,,,,
d = 45345
e = 0,5,6,8,,,,,,,
I want to filter the csv file so that it removes the ,,,,,, and then reformat this so that it will then become
a = 5464; b = 657578,0.46454,0,85976; c = 46; d = 45345; e = 0,5,6,8;
in once cell
I have the idea to use a regexp to filter the commas but I dont know how to filter it specifically.
  댓글 수: 2
Walter Roberson
Walter Roberson 2015년 9월 17일
When you say that they are in one cell each, do you mean one line each? Or do you mean that in the CSV file each of these expressions was enclosed in double-quotes "" to mark that the commas did not indicate the end of the value?
Rodriguez Pham
Rodriguez Pham 2015년 9월 17일
편집: Rodriguez Pham 2015년 9월 17일
So when I load the csv file into the matlab workspace using uigetfile, then fopen, then textscan, the variable are in a cell type of a 5x1 vector. So when I apply my opperation the result I want is a 1x1 with all of the varibles in that specfic format where it follows the 5 variables in a line.
The goal for me is to understand the syntax to do this operation because I will have lets say a 100x1 with variables assigned but having this comma issue and I want to apply my operation so that it will end up as a 20x1 with 5 variables in each row but 'one cell' for each row which I will then write to a text file. Does that make sense?

답변 (1개)

Walter Roberson
Walter Roberson 2015년 9월 17일
data = {'a = 5464,,,,,,,'; 'b = 657578,0.46454,0,85976'; 'c = 46,,,,,, '; 'd = 45345'; 'e = 0,5,6,8,,,,,,,'}
trimmed_data = regexprep(data, ',+\s*$', '');
joined_data = [strjoin(trimmed_data(:).', '; '), ';'];
  댓글 수: 1
Kirby Fears
Kirby Fears 2015년 9월 18일
Rodriguez,
Your answer is right here.

이 질문은 마감되었습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by