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
0 个评论
采纳的回答
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
更多回答(2 个)
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
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
2022-1-30
6 个评论
Image Analyst
2022-1-30
Not sure I'll get to it today. Some football games to watch and guests coming over soon.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!