我在本地驅動器中下載了youe csv文件。運行代碼並使用對話框選擇csv文件。
clear
clc
[file_name, mach_path] = uigetfile(...
{'*.csv', 'All CSV (*.csv)'}, ...
'Select File');
% If "Cancel" is selected then return
if isequal([file_name,mach_path],[0,0])
return
% Otherwise construct the fullfilename and Check and load the file
else
fileName = fullfile(mach_path,file_name);
end
fid = fopen(fileName,'r'); %# Open the file
lineArray = cell(100,1); %# Preallocate a cell array (ideally slightly
%# larger than is needed)
lineIndex = 1; %# Index of cell to place the next line in
nextLine = fgetl(fid); %# Read the first line from the file
while ~isequal(nextLine,-1) %# Loop while not at the end of the file
lineArray{lineIndex} = nextLine; %# Add the line to the cell array
lineIndex = lineIndex+1; %# Increment the line index
nextLine = fgetl(fid); %# Read the next line from the file
end
fclose(fid); %# Close the file
lineArray = lineArray(1:lineIndex-1); %# Remove empty cells, if needed
for iLine = 1:lineIndex-1 %# Loop over lines
lineData = textscan(lineArray{iLine},'%s',... %# Read strings
'Delimiter',',');
lineData = lineData{1}; %# Remove cell encapsulation
if strcmp(lineArray{iLine}(end),',') %# Account for when the line
lineData{end+1} = ''; %# ends with a delimiter
end
lineArray(iLine,1:numel(lineData)) = lineData; %# Overwrite line data
end
A = lineArray;
uniqueVals = unique(A(:,1));
[cc ~] = size(uniqueVals);
for i=1:cc
[mm ~]= size(find(ismember(A(:,1),uniqueVals(i))));
if mm>1
second = find(ismember(A(:,1),uniqueVals(i)));
disp('same value detected in rows: ')
disp(second(2));
A(second(2),:) = [];
disp(A);
end
end
我不能得到這個工作。 它不會在colum中檢測到相同的值,然後將其刪除並顯示錯誤消息 – Ryan
您可以向我發送錯誤消息嗎? – Javidan
我不知道如何把它寫proporly所以它不會搞砸 VAR1 VAR2 VAR3 VAR4 VAR5 Var6 'S123456' 'BN' 7 2 10 12 'S163765' '阿燕' 7 10 10 12 'S185216' 'Jyan' 4 2 -3 12 'S854789' '吉安' 7 2 7 7 'S325874' 'Zyan' 7 2 10 2 'S963256' 'Byan' 12 2 10 12 'S123456''名稱'7 2 4 4 'S214789''Hyan'10 7 10 12 – Ryan