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
4 件のコメント
その他の回答 (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!