Info
此问题已关闭。 请重新打开它进行编辑或回答。
How to used a Matlab generated function from text to a metrix table, then using this table to plot graph on diff axis in a custom Gui. thanks.
1 次查看(过去 30 天)
显示 更早的评论
_*I have all the codes I am working on bellow, and the GUI and data is attach. How can I plot different column in either x or y axis by selecting the data directly from the gui. Thanks_
*****************************************Code******************************************
function test1011 = importfile(filename, startRow, endRow)
%IMPORTFILE Import numeric data from a text file as a matrix.
% TEST1011 = IMPORTFILE(FILENAME) Reads data from text file FILENAME for
% the default selection.
%
% TEST1011 = IMPORTFILE(FILENAME, STARTROW, ENDROW) Reads data from rows
% STARTROW through ENDROW of text file FILENAME.
%
% Example:
% test1011 = importfile('test101.txt', 2, 31);
%
% See also TEXTSCAN.
% Auto-generated by MATLAB on 2017/08/08 12:20:15
%% Initialize variables. delimiter = '\t'; if nargin<=2 startRow = 2; endRow = inf; end
%% Read columns of data as strings: % For more information, see the TEXTSCAN documentation. formatSpec = '%s%s%s%s%s%s%s%s%s%[^\n\r]';
%% Open the text file. [file,path] = uigetfile('*.txt','Find the File to Import'); % raed table .txt
filename=[path,file];
fileID = fopen(filename,'r');
%% Read columns of data according to format string. % 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, endRow(1)-startRow(1)+1, 'Delimiter', delimiter, 'HeaderLines', startRow(1)-1, 'ReturnOnError', false); for block=2:length(startRow) frewind(fileID); dataArrayBlock = textscan(fileID, formatSpec, endRow(block)-startRow(block)+1, 'Delimiter', delimiter, 'HeaderLines', startRow(block)-1, 'ReturnOnError', false); for col=1:length(dataArray) dataArray{col} = [dataArray{col};dataArrayBlock{col}]; end end
%% Close the text file. fclose(fileID);
%% Convert the contents of columns containing numeric strings to numbers. % Replace non-numeric strings with NaN. raw = repmat({''},length(dataArray{1}),length(dataArray)-1); for col=1:length(dataArray)-1 raw(1:length(dataArray{col}),col) = dataArray{col}; end numericData = NaN(size(dataArray{1},1),size(dataArray,2));
for col=[1,4,5,7,9] % Converts strings in the input cell array to numbers. Replaced non-numeric % strings 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 any(numbers==',');
thousandsRegExp = '^\d+?(\,\d{3})*\.{0,1}\d*$';
if isempty(regexp(numbers, thousandsRegExp, 'once'));
numbers = NaN;
invalidThousandsSeparator = true;
end
end
% Convert numeric strings to numbers.
if ~invalidThousandsSeparator;
numbers = textscan(strrep(numbers, ',', ''), '%f');
numericData(row, col) = numbers{1};
raw{row, col} = numbers{1};
end
catch me
end
end
end
dateFormatIndex = 1; blankDates = cell(1,size(raw,2)); anyBlankDates = false(size(raw,1),1); invalidDates = cell(1,size(raw,2)); anyInvalidDates = false(size(raw,1),1); for col=[2,3]% Convert the contents of columns with dates to MATLAB datetimes using date format string. try dates{col} = datetime(dataArray{col}, 'Format', 'MM/dd/yyyy HH:mm', 'InputFormat', 'MM/dd/yyyy HH:mm'); %#ok<SAGROW> catch try % Handle dates surrounded by quotes dataArray{col} = cellfun(@(x) x(2:end-1), dataArray{col}, 'UniformOutput', false); dates{col} = datetime(dataArray{col}, 'Format', 'MM/dd/yyyy HH:mm', 'InputFormat', 'MM/dd/yyyy HH:mm'); %%#ok<SAGROW> catch dates{col} = repmat(datetime([NaN NaN NaN]), size(dataArray{col})); %#ok<SAGROW> end end
dateFormatIndex = dateFormatIndex + 1;
blankDates{col} = cellfun(@isempty, dataArray{col});
anyBlankDates = blankDates{col} | anyBlankDates;
invalidDates{col} = isnan(dates{col}.Hour) - blankDates{col};
anyInvalidDates = invalidDates{col} | anyInvalidDates;
end
dates = dates(:,[2,3]);
blankDates = blankDates(:,[2,3]);
invalidDates = invalidDates(:,[2,3]);
%% Split data into numeric and cell columns. rawNumericColumns = raw(:, [1,4,5,7,9]); rawCellColumns = raw(:, [6,8]);
%% Replace non-numeric cells with NaN R = cellfun(@(x) ~isnumeric(x) && ~islogical(x),rawNumericColumns); % Find non-numeric cells rawNumericColumns® = {NaN}; % Replace non-numeric cells
%% Create output variable test1011 = table; test1011.PROD_ID = cell2mat(rawNumericColumns(:, 1)); test1011.BEG_CPTR_TS = dates{:, 1}; test1011.END_CPTR_TS = dates{:, 2}; test1011.BEG_ENGN_HOURS = cell2mat(rawNumericColumns(:, 2)); test1011.END_ENGN_HOURS = cell2mat(rawNumericColumns(:, 3)); test1011.AUTO_EXHAUST_FILTER_CLEANING = rawCellColumns(:, 1); test1011.AVG_BAROMETRIC_PRESSURE = cell2mat(rawNumericColumns(:, 4)); test1011.AVG_COOLANT_TEMP = rawCellColumns(:, 2); test1011.AVG_DEF_CONSUMPTION_RATE = cell2mat(rawNumericColumns(:, 5));
%plot(test1011.AVG_BAROMETRIC_PRESSURE,test1011.END_ENGN_HOURS) %for test only
% For code requiring serial dates (datenum) instead of datetime, uncomment % the following line(s) below to return the imported dates as datenum(s).
% test1011.BEG_CPTR_TS=datenum(test1011.BEG_CPTR_TS); % test1011.END_CPTR_TS=datenum(test1011.END_CPTR_TS);
0 个评论
回答(0 个)
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!