Hi I keep getting an error with this:
%% generate sample data
K = 3;
numObservarations = 12000;
dimensions = 20;
data = fopen('M.dat','rt');
C = textscan(data,[numObservarations dimensions]);
??? Error using ==> textscan
Second input must be empty or a format string.
I tryed this method:
%% format data
%# read the list of features
fid = fopen('kddcup.names','rt');
C = textscan(fid, '%s %s', 'Delimiter',':', 'HeaderLines',1);
fclose(fid);
%# determine type of features
C{2} = regexprep(C{2}, '.$',''); %# remove "." at the end
attribNom = [ismember(C{2},'symbolic');true]; %# nominal features
%# build format string used to read/parse the actual data
frmt = cell(1,numel(C{1}));
frmt( ismember(C{2},'continuous') ) = {'%f'}; %# numeric features: read as number
frmt( ismember(C{2},'symbolic') ) = {'%s'}; %# nominal features: read as string
frmt = [frmt{:}];
frmt = [frmt '%s']; %# add the class attribute
%# read dataset
fid = fopen('kddcup.data_10_percent_corrected','rt');
C = textscan(fid, frmt, 'Delimiter',',');
fclose(fid);
%# convert nominal attributes to numeric
ind = find(attribNom);
G = cell(numel(ind),1);
for i=1:numel(ind)
[C{ind(i)},G{i}] = grp2idx( C{ind(i)} );
end
%# all numeric dataset
M = cell2mat(C);
data = M;
%% generate sample data
K = 3;
numObservarations = 12000;
dimensions = 20;
data = textscan([numObservarations dimensions]);
%% cluster
opts = statset('MaxIter', 500, 'Display', 'iter');
[clustIDX, clusters, interClustSum, Dist] = kmeans(data, K, 'options',opts, ...
'distance','sqEuclidean', 'EmptyAction','singleton', 'replicates',3);
%% plot data+clusters
figure, hold on
scatter3(data(:,1),data(:,2),data(:,3), 50, clustIDX, 'filled')
scatter3(clusters(:,1),clusters(:,2),clusters(:,3), 200, (1:K)', 'filled')
hold off, xlabel('x'), ylabel('y'), zlabel('z')
%% plot clusters quality
figure
[silh,h] = silhouette(data, clustIDX);
avrgScore = mean(silh);
%% Assign data to clusters
% calculate distance (squared) of all instances to each cluster centroid
D = zeros(numObservarations, K); % init distances
for k=1:K
%d = sum((x-y).^2).^0.5
D(:,k) = sum( ((data - repmat(clusters(k,:),numObservarations,1)).^2), 2);
end
% find for all instances the cluster closet to it
[minDists, clusterIndices] = min(D, [], 2);
% compare it with what you expect it to be
sum(clusterIndices == clustIDX)
but get the error:
??? Error using ==> textscan
Invalid file identifier. Use fopen to
generate a valid file identifier.
Error in ==> kmeans at 37
data = textscan([numObservarations
dimensions]);
Your call of textscan is not compliant with the syntax required. The following signatures are valid:
C = textscan(fid, 'format')
C = textscan(fid, 'format', N)
C = textscan(fid, 'format', 'param', value)
C = textscan(fid, 'format', N, 'param', value)
C = textscan(str, ...)
[C, position] = textscan(...)
Related
I want to get Velocity from Acceleration data(10 Hz) in MATLAB. I have the real Constant Velocity that I've calculated by (DeltaX/DeltaT). The problem is that when I use the code below, the graph has lot's of differences with the Real Velocity Graph. I've used both cumsum and omega arithmetic, could someone tell me if there is any problem in my codes of any of both methods? Thank you in advance.
link to access txt files: https://spaces.hightail.com/space/19rl7
%%%%%%%%%%%%%%% cumsum METHOD %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fid1 = fopen( 'Acceleration_data.txt', 'r' );
fid2 = fopen( 'Actual_Velocity_data.txt', 'r' );
formatSpec = '%f';
vel_Fast_Running = fscanf(fid2,formatSpec);
acc_Fast_Running = fscanf(fid1,formatSpec);
Fs=1000;
Ns = 10;%% Sampling freq
T = 1/Fs; %% Period
L = length(acc_Fast_Running); %% signal length
acceleration_data=acc_Fast_Running-median(acc_Fast_Running);
[B,A] = butter(6,0.098,'high'); %% highpass filter
a_filtr = filtfilt(B,A,acc_Fast_Running); %% acceleratiof w/o DC
v_cumsum = cumsum(a_filtr)*0.1;%integration
v_cumsum = v_cumsum - mean(v_cumsum);
[B,A] = butter(3,0.12,'low');
v_cumsum_filtr = filtfilt(B,A,v_cumsum);
%subplot(1,2,2);plot([v_cumsum' v_cumsum_filtr']);
hold on;plot(vel_Fast_Running)
plot(v_cumsum_filtr)
legend('Actual Vel','Vel From integration')
xlabel('Time')
ylabel('Velocity m/s')
%%%%%%%%%%%%%%% OMEGA ARITHMETIC METHOD %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fid1 = fopen( 'Acceleration_data.txt', 'r' );
fid2 = fopen( 'Actual_Velocity_data.txt', 'r' );
formatSpec = '%f';
vel_Fast_Running = fscanf(fid2,formatSpec);
acc_Fast_Running = fscanf(fid1,formatSpec);
Fs=1000;
Ns = 10;%% Sampling freq
T = 1/Fs; %% Period
L = length(acc_Fast_Running ); %% signal length
%t=(1:L)/Fs;
f = Fs*(0:L-1)/L;
acc_Fast_Running =acc_Fast_Running -mean(acc_Fast_Running );
[B,A] = butter(6,0.098,'high'); %% highpass filter
a_filtr = filtfilt(B,A,acc_Fast_Running);
Y = (fft(a_filtr)); % Fast fourier Transform
for i = 1 : L
if f(i) ~= 0 && f(i) < Fs/2;
Yf(i) = Y(i)/(2*pi*f(i)*sqrt(-1)) ;
else
Yf(i) = 0;
end
end
Yf(1:100)=0; %%
d_oa = real(ifft(Yf));
d1 = designfilt('lowpassiir','FilterOrder',4, ...
'HalfPowerFrequency',0.1,'DesignMethod','butter');
y = filtfilt(d1,d_oa);
figure(15)
%plot( d_oa)
subplot(2,1,1)
plot(y)
xlabel('Time')
ylabel('Vel be Omega Method')
subplot(2,1,2)
plot(vel_Fast_Running )
xlabel('Time')
ylabel('Actual Velocity m/s')
The code is for video processing in MATLAB and I have a problem in the first loop. I do not know what the problem is but the error that MATLAB gives is :
Assignment has more non-singleton rhs dimensions than non-singleton subscripts
Error in fi (line 34)
data(:,:,f) = I;
Here is my code:
clc;
close all;
clear all;
It1c = imread( '\\icnas1.cc.ic.ac.uk\fi15\Desktop\frames\Frames_V11\051.png' );
It600c = imread( '\\icnas1.cc.ic.ac.uk\fi15\Desktop\frames\Frames_V11\009.png' );
resf = 0.27e-6;
fr_r = 12000; %frame rate = 12000 fps
figure();
imagesc(It1c);
figure();
imagesc(It600c);
listing = dir('\\icnas1.cc.ic.ac.uk\fi15\Desktop\frames\Frames_V11\*.png');
N = 51;
data = zeros(624,1024,N);
for f = 1:N,
f
I = imread(['Frames_V11\',fullfile(listing(f).name)] );
data(:,:,f) = I;
end
figure; %see frames
for i = 1:N,
imagesc(data(:,:,i));
colorbar;
pause(0.1);
end
figure; %see frames
for i = 1:N,
imagesc(data(:,:,i)-data(:,:,1));
colorbar;
pause(0.1);
end
for i = 1:N,
i
data2(:,:,i) = data(:,:,i)-data(i);
end
figure; %see frames
for i = 1:N,
imagesc(data2(:,:,i));
colorbar;
pause(0.1);
end
figure;
imagesc(squeeze( mean(data2(230:270,:,:),1) ));
figure;
plot(squeeze(mean(mean(data5(210:235,395:425,:),1),2)));
Your image data is likely RGB and therefore has the following dimensions [nRows, nCols, nChannels] where nChannels is likely 3. The error is because you're trying to assign this 3D matrix to a 2D slice in data.
You need to therefore concatenate all of the images along the fourth dimension instead of the third.
data = zeros(624, 1024, 3, N);
for f = 1:N
data(:,:,:,f) = imread(['Frames_V11\',fullfile(listing(f).name)]);
end
i have to extract each characters from a image here i am uploading the code it is segmenting the horizontal lines but not able to segment the each characters along with the horizontal line segmentation loop. some1 please help to correct the code
this is the previous code:
%%horizontal histogram
H = sum(rotatedImage, 2);
darkPixels = H < 100; % Threshold
% label
[labeledRegions, numberOfRegions] = bwlabel(darkPixels);
fprintf('Number of regions = %d\n', numberOfRegions);
% Find centroids
measurements = regionprops(labeledRegions, 'Centroid');
% Get them into an array
allCentroids = [measurements.Centroid];
xCentroids = int32(allCentroids(1:2:end));
yCentroids = int32(allCentroids(2:2:end));
% Now you can just crop out some line of text you're interested in, into a separate image:
hold off;
plotLocation = 8;
for band = 1 : numberOfRegions-1
row1 = yCentroids(band);
row2 = yCentroids(band+1);
thisLine = rotatedImage(row1 : row2, :);
subplot(7, 2, plotLocation)
imshow(thisLine, [])
%% Let's compute and display the histogram.
verticalProjection = sum(thisLine, 2);
set(gcf, 'NumberTitle', 'Off')
t = verticalProjection;
t(t==0) = inf;
mayukh=min(t);
% 0 where there is background, 1 where there are letters
letterLocations = verticalProjection > mayukh;
% Find Rising and falling edges
d = diff(letterLocations);
startingRows = find(d>0);
endingRows = find(d<0);
% Extract each region
y=1;
for k = 1 : length(startingRows)
% Get sub image of just one character...
subImage = thisLine(:, startingRows(k):endingRows(k));
[L,num] = bwlabel(subImage);
for z= 1 : num
bw= ismember( L, z);
% Construct filename for this particular image.
baseFileName = sprintf('templates %d.png', y);
y=y+1;
% Prepend the folder to make the full file name.
fullFileName = fullfile('C:\Users\Omm\Downloads\', baseFileName);
% Do the write to disk.
imwrite(bw, fullFileName);
pause(2);
imshow(bw);
pause(5)
end;
y=y+2;
end;
plotLocation = plotLocation + 2;
end
but not segmenting the whole lines
Why don't you simply use regionprops with 'Image' property?
img = imread('http://i.stack.imgur.com/zpYa5.png'); %// read the image
bw = img(:,:,1) > 128; %// conver to mask
Use some minor morphological operations to handle spurious pixels
dbw = imdilate(bw, ones(3));
lb = bwlabel(dbw).*bw; %// label each character as a connected component
Now you can use regionprops to get each image
st = regionprops( lb, 'Image' );
Visualize the results
figure;
for ii=1:numel(st),
subplot(4,5,ii);
imshow(st(ii).Image,'border','tight');
title(num2str(ii));
end
I have the following code where I have two lines (L1 and L2) and I can find the point of intersection between the lines. However, L1 never changes but L2 does. This is where I am having the problem. I have a .txt file with a list of x and y coordinates. How do I alter the code such that I can swap out the figures 660 and 122.75 in L2 for the top two numbers in my list. Then save the intersection point and start again but this time replacing the two numbers with the second set of numbers in my list.
Regards,
Jer
clc;
clear;
%% Load data
filename = 'C:\blade1_data.txt';
delimiter = ' ';
formatSpec = '%f%f%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'MultipleDelimsAsOne', true, 'ReturnOnError', false);
fclose(fileID);
X_coords = dataArray{:, 1};
Y_coords = dataArray{:, 2};
clearvars filename delimiter formatSpec fileID dataArray ans;
%% Find intersection between lines
L1 = [656.25 122.75; 611.625 378.875]; % Defines rotor line
L2 = [0 122.75; 660 122.75]; % Defines coordinate where blade tip was located
dx = diff(L1); %# Take the differences down each column
dy = diff(L2);
den = dx(1)*dy(2)-dy(1)*dx(2); %# Precompute the denominator
ua = (dx(2)*(L2(1)-L2(3))-dy(2)*(L1(1)-L1(3)))/den;
ub = (dx(1)*(L2(1)-L2(3))-dy(1)*(L1(1)-L1(3)))/den;
% Intersection point of the two lines:
xi = L1(1)+ua*dx(1);
yi = L2(1)+ua*dy(1);
Working code
clc;
clear;
%% Load data
filename = 'C:data.txt';
delimiter = ' ';
formatSpec = '%f%f%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'MultipleDelimsAsOne', true, 'ReturnOnError', false);
fclose(fileID);
X_coords = dataArray{:, 1};
Y_coords = dataArray{:, 2};
clearvars filename delimiter formatSpec fileID dataArray ans;
%% Find intersection between lines
nfiles = length(X_coords);
for n = 1: nfiles;
ptx = X_coords(n:n,1);
pty = Y_coords(n:n,1);
L1 = [656.25 122.75; 611.625 378.875]; % Defines rotor line
L2 = [0 pty; ptx pty]; % Defines coordinate where blade tip was located
dx = diff(L1); %# Take the differences down each column
dy = diff(L2);
den = dx(1)*dy(2)-dy(1)*dx(2); %# Precompute the denominator
ua = (dx(2)*(L2(1)-L2(3))-dy(2)*(L1(1)-L1(3)))/den;
ub = (dx(1)*(L2(1)-L2(3))-dy(1)*(L1(1)-L1(3)))/den;
% Intersection point of the two lines:
xi = L1(1)+ua*dx(1);
yi = L2(1)+ua*dy(1);
data(n,:) = [xi yi];
end
Hi I was wondering when you cluster data on the figure screen is there a way to show which rows the data points belong to when you scroll over them?
From the picture above I was hoping there would be a way in which if I select or scroll over the points that I could tell which row it belonged to.
Here is the code:
%% dimensionality reduction
columns = 6
[U,S,V]=svds(fulldata,columns);
%% randomly select dataset
rows = 1000;
columns = 6;
%# pick random rows
indX = randperm( size(fulldata,1) );
indX = indX(1:rows);
%# pick random columns
indY = randperm( size(fulldata,2) );
indY = indY(1:columns);
%# filter data
data = U(indX,indY);
%% apply normalization method to every cell
data = data./repmat(sqrt(sum(data.^2)),size(data,1),1);
%% generate sample data
K = 6;
numObservarations = 1000;
dimensions = 6;
%% cluster
opts = statset('MaxIter', 100, 'Display', 'iter');
[clustIDX, clusters, interClustSum, Dist] = kmeans(data, K, 'options',opts, ...
'distance','sqEuclidean', 'EmptyAction','singleton', 'replicates',3);
%% plot data+clusters
figure, hold on
scatter3(data(:,1),data(:,2),data(:,3), 5, clustIDX, 'filled')
scatter3(clusters(:,1),clusters(:,2),clusters(:,3), 100, (1:K)', 'filled')
hold off, xlabel('x'), ylabel('y'), zlabel('z')
%% plot clusters quality
figure
[silh,h] = silhouette(data, clustIDX);
avrgScore = mean(silh);
%% Assign data to clusters
% calculate distance (squared) of all instances to each cluster centroid
D = zeros(numObservarations, K); % init distances
for k=1:K
%d = sum((x-y).^2).^0.5
D(:,k) = sum( ((data - repmat(clusters(k,:),numObservarations,1)).^2), 2);
end
% find for all instances the cluster closet to it
[minDists, clusterIndices] = min(D, [], 2);
% compare it with what you expect it to be
sum(clusterIndices == clustIDX)
Or possibly an output method of the clusters data, normalized and re-organized to there original format with appedicies on the end column with which row it belonged to from the original "fulldata".
You could use the data cursors feature which displays a tooltip when you select a point from the plot. You can use a modified update function to display all sorts of information about the point selected.
Here is a working example:
function customCusrorModeDemo()
%# data
D = load('fisheriris');
data = D.meas;
[clustIdx,labels] = grp2idx(D.species);
K = numel(labels);
clr = hsv(K);
%# instance indices grouped according to class
ind = accumarray(clustIdx, 1:size(data,1), [K 1], #(x){x});
%# plot
%#gscatter(data(:,1), data(:,2), clustIdx, clr)
hLine = zeros(K,1);
for k=1:K
hLine(k) = line(data(ind{k},1), data(ind{k},2), data(ind{k},3), ...
'LineStyle','none', 'Color',clr(k,:), ...
'Marker','.', 'MarkerSize',15);
end
xlabel('SL'), ylabel('SW'), zlabel('PL')
legend(hLine, labels)
view(3), box on, grid on
%# data cursor
hDCM = datacursormode(gcf);
set(hDCM, 'UpdateFcn',#updateFcn, 'DisplayStyle','window')
set(hDCM, 'Enable','on')
%# callback function
function txt = updateFcn(~,evt)
hObj = get(evt,'Target'); %# line object handle
idx = get(evt,'DataIndex'); %# index of nearest point
%# class index of data point
cIdx = find(hLine==hObj, 1, 'first');
%# instance index (index into the entire data matrix)
idx = ind{cIdx}(idx);
%# output text
txt = {
sprintf('SL: %g', data(idx,1)) ;
sprintf('SW: %g', data(idx,2)) ;
sprintf('PL: %g', data(idx,3)) ;
sprintf('PW: %g', data(idx,4)) ;
sprintf('Index: %d', idx) ;
sprintf('Class: %s', labels{clustIdx(idx)}) ;
};
end
end
Here is how it looks like in both 2D and 3D views (with different display styles):