how to delete permanently the first row of this table

조회 수: 120 (최근 30일)
MA
MA 2021년 9월 15일
댓글: MA 2021년 9월 16일
Hi, This table was baiscally a data file that I have imported using 'import data tool', I woud like to get rid of the first row in this tabel contaning tthe non values but I am not sure how to do that, because every time I try to delete it when I run the code again it returs back.. so what can I do?
  댓글 수: 2
Cris LaPierre
Cris LaPierre 2021년 9월 15일
Share your code and data file.
MA
MA 2021년 9월 15일
편집: Cris LaPierre 2021년 9월 15일
Thank you for replying to my question. I am basically using the script generated by matlab and here it is and the data file is attached (just aside note I am only using a specific columns from the data file not all of them)
%% Import data from text file.
% Script for importing data from the following text file:
%
% C:\Users\Windows 10 Pro\Documents\MATLAB\jro19661111.001.txt
%
% To extend the code to different selected data or a different text file, generate a function instead of a script.
% Auto-generated by MATLAB on 2021/09/15 17:22:30
%% Initialize variables.
filename = 'C:\Users\Windows 10 Pro\Documents\MATLAB\jro19661111.001.txt';
%% Read columns of data as text:
% For more information, see the TEXTSCAN documentation.
formatSpec = '%6s%10s%10s%10s%10s%*10*s%*13*s%*11*s%*14*s%*14*s%*14s%11s%14s%[^\n\r]';
%% Open the text file.
fileID = fopen(filename,'r');
%% Read columns of data according to the format.
% This call is based on the structure of the file used to generate this code. If an error occurs for a different file, try regenerating the code from the Import Tool.
dataArray = textscan(fileID, formatSpec, 'Delimiter', '', 'WhiteSpace', '', 'TextType', 'string', 'ReturnOnError', false);
%% Close the text file.
fclose(fileID);
%% Convert the contents of columns containing numeric text to numbers.
% Replace non-numeric text with NaN.
raw = repmat({''},length(dataArray{1}),length(dataArray)-1);
for col=1:length(dataArray)-1
raw(1:length(dataArray{col}),col) = mat2cell(dataArray{col}, ones(length(dataArray{col}), 1));
end
numericData = NaN(size(dataArray{1},1),size(dataArray,2));
for col=[1,2,3,4,5,6,7]
% Converts text in the input cell array to numbers. Replaced non-numeric text with NaN.
rawData = dataArray{col};
for row=1:size(rawData, 1)
% Create a regular expression to detect and remove non-numeric prefixes and suffixes.
regexstr = '(?<prefix>.*?)(?<numbers>([-]*(\d+[\,]*)+[\.]{0,1}\d*[eEdD]{0,1}[-+]*\d*[i]{0,1})|([-]*(\d+[\,]*)*[\.]{1,1}\d+[eEdD]{0,1}[-+]*\d*[i]{0,1}))(?<suffix>.*)';
try
result = regexp(rawData(row), regexstr, 'names');
numbers = result.numbers;
% Detected commas in non-thousand locations.
invalidThousandsSeparator = false;
if numbers.contains(',')
thousandsRegExp = '^[-/+]*\d+?(\,\d{3})*\.{0,1}\d*$';
if isempty(regexp(numbers, thousandsRegExp, 'once'))
numbers = NaN;
invalidThousandsSeparator = true;
end
end
% Convert numeric text to numbers.
if ~invalidThousandsSeparator
numbers = textscan(char(strrep(numbers, ',', '')), '%f');
numericData(row, col) = numbers{1};
raw{row, col} = numbers{1};
end
catch
raw{row, col} = rawData{row};
end
end
end
%% Replace non-numeric cells with NaN
R = cellfun(@(x) ~isnumeric(x) && ~islogical(x),raw); % Find non-numeric cells
raw(R) = {NaN}; % Replace non-numeric cells
%% Create output variable
jro19661111 = table;
jro19661111.YEAR = cell2mat(raw(:, 1));
jro19661111.MONTH = cell2mat(raw(:, 2));
jro19661111.DAY = cell2mat(raw(:, 3));
jro19661111.HOUR = cell2mat(raw(:, 4));
jro19661111.MIN = cell2mat(raw(:, 5));
jro19661111.GDALT = cell2mat(raw(:, 6));
jro19661111.NE8 = cell2mat(raw(:, 7));
%% Clear temporary variables
clearvars filename formatSpec fileID dataArray ans raw col numericData rawData row regexstr result numbers invalidThousandsSeparator thousandsRegExp R;

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2021년 9월 15일
% Read table from txt file:
t = readtable('jro19661111.001.txt')
% Delete first row
t(1,:) = [];
  댓글 수: 2
Cris LaPierre
Cris LaPierre 2021년 9월 15일
편집: Cris LaPierre 2021년 9월 15일
To expound, your import code is reading in the column headers. If you want to use the import tool/textscan, set the range so that it skips the first line. If doing it programmatically, use the HeaderLines input argument.
MA
MA 2021년 9월 16일
thank you.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by