matlab数组存字符串,MATLAB字符串数组存储为CSV格式

直奔主题,需要保存处理图像的文件名(string)数组。本文只用作自己的实验记录,侵删。

搬运自matlab官网的代码:https://www.mathworks.com/matlabcentral/fileexchange/7601-cell2csv?s_tid=mwa_osa_a

以下为MATLAB cell array to csv的函数实现:

function cell2csv(filename,cellArray,delimiter)

% Writes cell array content into a *.csv file.

%

% CELL2CSV(filename,cellArray,delimiter)

%

% filename = Name of the file to save. [ i.e. 'text.csv' ]

% cellarray = Name of the Cell Array where the data is in

% delimiter = seperating sign, normally:',' (it's default)

%

% by Sylvain Fiedler, KA, 2004

% modified by Rob Kohr, Rutgers, 2005 - changed to english and fixed delimiter

if nargin<3

delimiter = ',';

end

datei = fopen(filename,'w');

for z=1:size(cellArray,1)

for s=1:size(cellArray,2)

var = eval(['cellArray{z,s}']);

if size(var,1) == 0

var = '';

end

if isnumeric(var) == 1

var = num2str(var);

end

fprintf(datei,var);

if s ~= size(cellArray,2)

fprintf(datei,[delimiter]);

end

end

fprintf(datei,'\n');

end

fclose(datei);

我的调用代码实例:

clc;

clear all;

path = 'E:\DZY\CASIA\train\2\';

fileFolder = fullfile(path);

dirOutput = dir(fullfile(fileFolder,'*.jpg'));

fileNames = {dirOutput.name};

list = string(fileNames);

n = length(fileNames);

Train_images = [];

% 创建字符串数组

Train_names = strings;

for i = 1:n

% 拼接图片的路径

pic_path = strcat(path, list(i));

pic_path = char(pic_path);

% fk1是自己写的特征提取代码,返回一个1*256的特征向量

lpq_gray = fk1(pic_path);

% 组成样本集

Train_images(i,:) = lpq_gray;

Train_names(i,:) = list(i);

disp('processing :' + string(pic_path));

end

% 存储文件名

save_path_data = 'E:\DZY\CASIA\ycbcr\Test0.csv';

save_path_name = 'E:\DZY\CASIA\ycbcr\Test0_name.csv';

csvwrite(save_path_data,Train_images);

%将文件名数组保存

cell2csv(save_path_name, Train_names, ',');

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值