need to read a mixed csv file

11 次查看(过去 30 天)
I would like to read the csv file attached and generate the following variables
MAIN_FOLDER ="V:\OPTIMIZATION_DEPOT\AntGod\"
SUB_FOLDER="V:\OPTIMIZATION_DEPOT\AntGod\AntGODorig_20211223_0528\O_20220110_0614_Yaqing_Optimization_OMEGA002\"
MACHINE="V:\ "
PROJECT_NAME="20220110_0614_Yaqing_Optimization"
Variable_Names =[ "sk_outer_polyrod" "sk_inner_polyrod" "sk" "n_offset" "k15" "k14" "k13" "k12" "k11" "k10 "]; (strings)
Variable_Units =[ "Nan" "Nan" "mm" "mm" "mm" "mm" "mm" "mm" "mm" "mm" ]; (strings)
Initial_Value =[0.75 0.85 0.08 5 15 32 61 91 86 80 ]; (floating numbers)
Minimum_Value =[ 0.7 0.7 0.06 3.75 11.25 24 45.75 68.25 64.5 60 ]; (floating numbers)
Maximal_Value =[ 1.5 1.4 0.3 7 20 42 80 120 120 110 ] (floating numbers)
I really would like to have this in csv format so I can easily edit it with either microsoft excel or Libreoffice calc. It's supposed to be an input file for an optimization setup. I tried many options (readcsv, readxlsx,csv2struct, etc) but none of them allowed me to achieve this
Thank you

采纳的回答

Voss
Voss 2022-1-30
编辑:Voss 2022-1-30
Try this. It will create a struct (called 'vars') with fields corresponding to the variables you specified. (If you really want to make those variables in your workspace, you can do that using eval() or assignin() on each field of the struct vars.)
It may be flexible enough to work on variations of the file you posted.
c = readcell('TEST.csv','TextType','string');
c(cellfun(@(x)any(ismissing(x)),c)) = {""};
idx = find([c{:,1}] == "Variable_Names",1);
data_exists = ~isempty(idx);
if ~data_exists
idx = size(c,1)+1;
end
vars = struct();
for i = 1:idx-1
try
vars.(c{i,1}) = c{i,2};
catch ME
disp(ME.message);
end
end
if data_exists
for j = 1:size(c,2)
try
vars.(c{idx,j}) = [c{idx+1:end,j}];
catch ME
disp(ME.message);
continue
end
if isstring(vars.(c{idx,j}))
vars.(c{idx,j})(arrayfun(@(x)x == "",vars.(c{idx,j}))) = "Nan";
end
end
end
vars
vars = struct with fields:
MAIN_FOLDER: "V:\OPTIMIZATION_DEPOT\AntGod\" SUB_FOLDER: "V:\OPTIMIZATION_DEPOT\AntGod\AntGODorig_20211223_0528\O_20220110_0614_Yaqing_Optimization_OMEGA002\" MACHINE: "V:\" PROJECT_NAME: "20220110_0614_Yaqing_Optimization" Variable_Names: ["sk_outer_polyrod" "sk_inner_polyrod" "sk" "n_offset" "k15" "k14" "k13" "k12" "k11" "k10"] Variable_Units: ["Nan" "Nan" "Nan" "mm" "mm" "mm" "mm" "mm" "mm" "mm"] Initial_Value: [0.7500 0.8500 0.0800 5 15 32 61 91 86 80] Minimum_Value: [0.7000 0.7000 0.0600 3.7500 11.2500 24 45.7500 68.2500 64.5000 60] Maximal_Value: [1.5000 1.4000 0.3000 7 20 42 80 120 120 110]
  4 个评论
NAFTALI HERSCOVICI
NAFTALI HERSCOVICI 2022-1-31
just now read your comments and advices. thank you
my experience actually tells me that I have to rely on files because a simulation can crash.
I had cases in which after a few days of optimization I lost everything in a blip because of a crash. the simulation engines I use are not very stable.
This is why I need to keep a journal, which gives me the option to restart the optimization from the point before the crash. just reading the journal would allow me tosave the time of recalculating all the iterations lost.
The one file for input/output, in this case, seems to me OK because the input role is played ONCE at the begining of the process. the rest is all output.

请先登录,再进行评论。

更多回答(2 个)

Image Analyst
Image Analyst 2022-1-29
Try this:
data = readcell('test.csv')
% MAIN_FOLDER ="V:\OPTIMIZATION_DEPOT\AntGod\"
% SUB_FOLDER="V:\OPTIMIZATION_DEPOT\AntGod\AntGODorig_20211223_0528\O_20220110_0614_Yaqing_Optimization_OMEGA002\"
% MACHINE="V:\ "
% PROJECT_NAME="20220110_0614_Yaqing_Optimization"
% Variable_Names =[ "sk_outer_polyrod" "sk_inner_polyrod" "sk" "n_offset" "k15" "k14" "k13" "k12" "k11" "k10 "]; (strings)
% Variable_Units =[ "Nan" "Nan" "mm" "mm" "mm" "mm" "mm" "mm" "mm" "mm" ]; (strings)
% Initial_Value =[0.75 0.85 0.08 5 15 32 61 91 86 80 ]; (floating numbers)
% Minimum_Value =[ 0.7 0.7 0.06 3.75 11.25 24 45.75 68.25 64.5 60 ]; (floating numbers)
% Maximal_Value =[ 1.5 1.4 0.3 7 20 42 80 120 120 110 ] (floating numbers)
SUB_FOLDER = data{2,2}
slashLocations = strfind(SUB_FOLDER, '\')
MAIN_FOLDER = SUB_FOLDER(1:slashLocations(3))
MACHINE = data{3, 2}
PROJECT_NAME = data{4, 2}
Variable_Names = data(6 : 15);
Variable_Units = data(6 : 15, 2);
for row = 6 : 15
Initial_Value(row - 5) = data{row, 3};
Minimum_Value(row - 5) = data{row, 4};
Maximal_Value(row - 5) = data{row, 5};
end
fprintf('Done!\n');
  4 个评论
Image Analyst
Image Analyst 2022-1-29
编辑:Image Analyst 2022-1-29
I literally used the file you attached and it worked fine. So you must be trying it on a different file that has a different format, like the things are in different locations, or there are extra things in the file, or things missing. Attach that file and we'll see how to make the code more flexible, within reason.
The error tells me that what's in data(row, 3) is not a single number like the example you uploaded. What is it? A vector? A matrix?
NAFTALI HERSCOVICI
NAFTALI HERSCOVICI 2022-1-30
You are right. it was a slightly different file.
Thank you, it works perfect

请先登录,再进行评论。


NAFTALI HERSCOVICI
NAFTALI HERSCOVICI 2022-1-30
Following question:
I was trying to update the file (for now just writing to another file) with
data(1,99)=textscan('gaga','%s','Delimiter',' ')';
data(2,99)=textscan("beach",'%s','Delimiter',' ')';
data(3,99)=num2cell(17.4);
writecell(data, 'C:\Users\tulih\OneDrive\OneDriveDocuments\MATLAB\TEST2.csv');
and I got this error:
Error using writecell (line 153)
Unsupported cell element of type 'missing'. Convert the element to numeric, logical, string, datetime,
duration, or categorical before writing.
not sure how to fix this
  6 个评论
Image Analyst
Image Analyst 2022-1-30
Not sure I'll get to it today. Some football games to watch and guests coming over soon.

请先登录,再进行评论。

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by