Problem in text to plot ....
1 次查看(过去 30 天)
显示 更早的评论
My Matlab code is :
clearvars -except
name = '47 T 0.5 BB';
inputfile = strcat(name,'.txt');
outputfile = strcat(name,'_FrictioncoeffvsCycle.txt');
[M,header] = txt2mat(inputfile);
distance = header (1);
velocity = header (2);
cycles = header (3);
samrate = header (4);
cutoff = 0.20; % percent of data on each side to cut off
% Split data into forwards and reverse cycles based on if the position is
% either increasing or decreasing. This also eliminates the data for any
% pauses in between reversals.
Mf = cell(cycles,1);
Mr = cell(cycles,1);
rowf = 0;
rowr = 0;
pagef = 0;
pager = 0;
for i = 1:length(M)-1
if M(i,7)<M(i+1,7)
if rowf==0
pagef = pagef+1;
rowr = 0;
end
rowf = rowf+1;
Mf{pagef} = [Mf{pagef}; M(i,:)];
elseif M(i,7)>M(i+1,7)
if rowr==0
pager = pager+1;
rowf = 0;
end
rowr = rowr+1;
Mr{pager} = [Mr{pager}; M(i,:)];
end
end
% Calculate mean and standard deviation for the normal force, friction
% force, and friction coefficient and export data to output file. Then plot
% friction coefficient vs cycle number.
for i = 1:cycles
outputarray(i,1) = i;
normal = -[Mf{i}(round(cutoff*length(Mf{i-1})):round((1-cutoff)*length(Mf{i-1})),3);
Mr{i-1}(round(cutoff*length(Mr{i-1})):round((1-cutoff)*length(Mr{i-1})),3)];
outputarray(i,2) = mean(normal);
outputarray(i,3) = std(normal);
friction = [Mf{i-1}(round(cutoff*length(Mf{i-1})):round((1-cutoff)*length(Mf{i-1})),1);
-Mr{i-1}(round(cutoff*length(Mr{i-1})):round((1-cutoff)*length(Mr{i-1})),1)];
outputarray(i,4) = mean(friction);
outputarray(i,5) = std(friction);
outputarray(i,6) = outputarray(i,4)/outputarray(i,2);
outputarray(i,7) = outputarray(i,6)*sqrt((outputarray(i,3)/outputarray(i,2))^2+(outputarray(i,5)/outputarray(i,4))^2);
end
fileID = fopen(outputfile,'wb');
fprintf(fileID, 'Cycle Number \t Average Normal Force (N) \t Normal Force Error (N) \t Average Friction Force (N) \t Friction Force Error (N) \t Friction Coefficient \t Friction Coefficient Error \n');
for i = 1:cycles
fprintf(fileID,'%d \t %d \t %d \t %d \t %d \t %d \t %d \n',outputarray(i,:));
end
fclose(fileID);
figure()
plot(outputarray(:,1),outputarray(:,6),'LineWidth',3)
ylim([0 0.5])
title(name,'FontSize',14)
xlabel('Cycle number','FontSize',14)
ylabel('Friction coefficient','FontSize',14)
function [M,header] = txt2mat(filename)
% Requires str2doubleq.mexw64. Reads filename and returns tabular numerical
% data in a matrix M padded with zeros as necessary to maintain dimensional
% consistency. Also returns non-tabular numerical data in an array header.
opts = detectImportOptions(filename);
t_vars = getVars(readtable(filename,opts));
t_varnames = fieldnames(t_vars);
for i = 1:length(t_varnames)
if isa(t_vars.(t_varnames{i}),'double')
M(:,i) = t_vars.(t_varnames{i});
else
M(:,i) = real(str2doubleq(t_vars.(t_varnames{i})));
end
end
M = M(:,any(M));
fileID = fopen(filename);
header = textscan(fileID,'%s',opts.DataLine-1,'Delimiter','\n');
fclose('all');
header = cell2mat(cellfun(@str2num,regexp(strjoin(header{:}),...
'-?\d+[.]?\d*((e|E)-?\d*)?','Match'),'UniformOutput',false));
end
This is the problem I am facing. Please help.
0 个评论
采纳的回答
Walter Roberson
2019-12-13
for i = 1:cycles
i starts at 1
normal = -[Mf{i}(round(cutoff*length(Mf{i-1})):round((1-cutoff)*length(Mf{i-1})),3);
Mf{i-1} starts at Mf{1-1} which is Mf{0} which is illegal
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Import and Analysis 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!