variation of columns in textscan

1 view (last 30 days)
Hi matlab gurus
I have a tricky question that I just can't "for loop" around. I have to make a program that can read an csv document containing alot of data. (Groundwaterlevel readings pr 5 min, over 2 years) "Its not important but now you know".
the csv file comes with two columns pr "reading equipment", and that can range from 1 to 20. So an example would be 4 measuring equipments, gives 8 columns.
The first column in the pair is the time and the second is the measurements. Thats why I have made the format for the textscan as:
formatSpec = '%{dd-MM-y HH:mm}D%q%[^\n\r]';
This is an example of 1 measuring equipment data set. But if there where two it would be:
formatSpec = '%{dd-MM-y HH:mm}D%q%{dd-MM-y HH:mm}D%q%[^\n\r]';
And so on...
I've found out that if I put a * in front of the columns of example measuring equipment 1:
formatSpec = '%*{dd-MM-y HH:mm}D%*q%{dd-MM-y HH:mm}D%q%[^\n\r]';
The textscan only gives me the data from the secound column which is nice. But here is where I have my troubles.
I need a code that can first measure how many measuring equipments there are: Easy length(data,2)/2 = number of measuring equipments.
But then I need a code that can produce the formatSpec so that if its 1
formatSpec = '%{dd-MM-y HH:mm}D%q%[^\n\r]';
2:
formatSpec = '%{dd-MM-y HH:mm}D%q%{dd-MM-y HH:mm}D%q%[^\n\r]';
3:
formatSpec = '%{dd-MM-y HH:mm}D%q%{dd-MM-y HH:mm}D%q%{dd-MM-y HH:mm}D%q%[^\n\r]';
and so on......
The second part I need to fix is that a user can choose which of the measuring equipments he wants the data from, for as an example there are 4 measuring equipments, so first the program reads that there are 4, and set the textscan to use a formatSpec for 4 inputs, secondly he chooses the 3 measuring equipment so the formatSpec will look like this:
formatSpec = '%*{dd-MM-y HH:mm}D%*q%*{dd-MM-y HH:mm}D%*q%{dd-MM-y HH:mm}D%q%*{dd-MM-y HH:mm}D%*q%[^\n\r]';
How do I make a code that can do that?
  1 Comment
Mikkel Ibsen
Mikkel Ibsen on 12 Dec 2017
This is the way I fixe it right now, really ugly coding, but can't figure out how to solve it, and this is getting really long and massiv around h = 10.
h = length(pejling)
text = '%{dd-MM-y HH:mm}D%q';
text2 = '%*{dd-MM-y HH:mm}D%*q';
ending = '%[^\n\r]';
if h == 1
formatSpec = [text ending];
elseif h == 2
if pejling_select == pejling(1)
formatSpec = [text text2 ending];
else
formatSpec = [text2 text ending];
end
elseif h == 3
if pejling_select == pejling(1)
formatSpec = [text text2 text2 ending];
elseif pejling_select == pejling(2)
formatSpec = [text2 text text2 ending];
else
formatSpec = [text2 text2 text ending];
end
end

Sign in to comment.

Accepted Answer

Chris Perkins
Chris Perkins on 14 Dec 2017
Edited: Chris Perkins on 14 Dec 2017
Hi Mikkel,
You can use the function "strcat" to programmatically combine smaller formats to build up your final formatSpec.
Here is an example, based on your requirements (you should be able to tailor this to fit your final use case):
% Declare Strings for use later
repeatedPartIncludeInstrument = '%{dd-MM-y HH:mm}D%q';
repeatedPartExcludeInstrument = '%*{dd-MM-y HH:mm}D%*q';
endPart = '%[^\n\r]';
% Set the number of times you loop executes
% Change this to be based on your length()
h = 5;
% Define the final formatSpec empty at first
finalFormat = '';
% List of instruments the user wants to use
% Set this based on how your store this data
instrumentsToUse = [2,3];
% Loop through the number of instruments
for count = 1:h
% Pick whether to include the instrument based on if the current value
% of count is included in the list of instruments to use
if any(ismember(count, instrumentsToUse))
% Use "strcat" to combine the variables
finalFormat = strcat(finalFormat, repeatedPartIncludeInstrument);
else
finalFormat = strcat(finalFormat, repeatedPartExcludeInstrument);
end
end
% Use "strcat" to add-on the end section
finalFormat = strcat(finalFormat, endPart);
% Just for sample code
disp(finalFormat);
Alternatively, you could look into using a "switch" statement instead of the many "if" and "elseif" statements you currently use. See the following documentation page for a description of "switch":

More Answers (0)

Community Treasure Hunt

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

Start Hunting!