need to read a mixed csv file
显示 更早的评论
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
采纳的回答
更多回答(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 个评论
NAFTALI HERSCOVICI
2022-1-29
NAFTALI HERSCOVICI
2022-1-29
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
NAFTALI HERSCOVICI
2022-1-30
0 个投票
6 个评论
Image Analyst
2022-1-30
There is at least one cell in the data cell array that contains null. writecell() doesn't like that. Scan the array and if the contents are empty, assign it to something, like a space or whatever you want.
for k = 1 : numel(data)
if isempty(data{k})
data{k} = ' ';
end
end
NAFTALI HERSCOVICI
2022-1-30
Image Analyst
2022-1-30
I can't do anything unless you upload the file, right? Did you forget to attach it?
NAFTALI HERSCOVICI
2022-1-30
Image Analyst
2022-1-30
Not sure I'll get to it today. Some football games to watch and guests coming over soon.
NAFTALI HERSCOVICI
2022-1-30
类别
在 帮助中心 和 File Exchange 中查找有关 R Language 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!