Thread Subject:
replacing imported non numeric cells with my own numbers

Subject: replacing imported non numeric cells with my own numbers

From: Joseph Frank

Date: 6 Jul, 2012 14:39:18

Message: 1 of 2

Hi,

I am importing CSV files. Matlab is giving me the option to replace non numeric cells by a specific value such as zero or nan. However, I want to have the option to assign multiple values depending on the content of the cell . For example: one column (which is column 13 of an 87000x15 Matrix" contains "T" or "C" or "CR" values and I want to tell matlab if the non mumeric value is "T" then assign the numeric value "1" , if it is "C" assign "0", if it is "CR" assign "2". I am wondering how I can modify the matlab script to get this option. The script auto-generated by Matlab contain the following:

%% Import data from spreadsheet
% Script for importing data from the following spreadsheet:
%
% Workbook: C:\Users\Joseph\Documents\MATLAB\CS\CSV\1.csv Worksheet: 1
%
% To extend the code to different selected data or a different spreadsheet,
% generate a function instead of a script.

% Auto-generated by MATLAB on 2012/07/06 17:23:27

%% Import the data, extracting spreadsheet dates in MATLAB datenum format
[~, ~, raw, dateNums] = xlsread('C:\Users\Wassim\Documents\MATLAB\CS\CSV\1.csv',num2str(i),'','',@convertSpreadsheetDates);
raw = raw(2:end,4:end);
dateNums = dateNums(2:end,4:end);

%% Replace date strings by MATLAB datenums
R = ~cellfun(@isequalwithequalnans,dateNums,raw) & cellfun('isclass',raw,'char'); % Find Excel dates
raw(R) = dateNums(R);

%% Replace non-numeric cells with 0.0
R = cellfun(@(x) ~isnumeric(x) || isnan(x),raw); % Find non-numeric cells
raw(R) = {0.0}; % Replace non-numeric cells

%% Create output variable
untitled = cell2mat(raw);
%% Clear temporary variables
clearvars raw dateNums R;

Thanks

Subject: replacing imported non numeric cells with my own numbers

From: dpb

Date: 7 Jul, 2012 14:40:53

Message: 2 of 2

On 7/6/2012 9:39 AM, joseph Frank wrote:
...
> I am importing CSV files. Matlab is giving me the option to replace non
> numeric cells by a specific value such as zero or nan. However, I want
> to have the option to assign multiple values depending on the content of
> the cell . For example: one column (which is column 13 of an 87000x15
> Matrix" contains "T" or "C" or "CR" values and I want to tell matlab if
> the non mumeric value is "T" then assign the numeric value "1" , if it
> is "C" assign "0", if it is "CR" assign "2". I am wondering how I can
> modify the matlab script to get this option. The script auto-generated
> by Matlab contain the following:
>
...

> [~, ~, raw, dateNums] =
> xlsread('C:\Users\...\CSV\1.csv',num2str(i),'','',@convertSpreadsheetDates);
> raw = raw(2:end,4:end);
...

String functions work on cell contents so I'd just do something like

ix=strfind(raw(:,13),'T'); raw(:,13){ix}=1;

(I think; my release doesn't have the multilevel indexing; guessing on
syntax some here)...

--

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
xlsread joseph Frank 6 Jul, 2012 10:44:25
import joseph Frank 6 Jul, 2012 10:44:25
rssFeed for this Thread

Contact us