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
Related
I am trying to figure out how to make a program in Matlab to detect edges of flames so I can compare their flame shape. I have been able to get this program to work half of the time. The program reads the files, coverts it to black and white, and then I have been having issues with the bwboundaries function (I think).
It will work some times and not others depending on the picture, they can be seen below.
Any help anyone could give me would be appreciated. I am struggling here to figure out what I am doing wrong.
clear, clc , close all
%% Specify the folder where the files live.
%myFolder = 'D:\Try';
myFolder = 'D:\Try\PicNamed\SpTestTry\hope';
%% Check to make sure that folder actually exists.
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
%% Get a list of all files in the folder with the pattern
filePattern = fullfile(myFolder, '*.jpg'); % Change to whatever need.
theFiles = dir(filePattern);
line = cell(length(theFiles), 1) ;
rValue = cell(length(theFiles),1);
baseFileName = theFiles(1).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
imageArray = imread(fullFileName);
% getting the rectanglur cords to crop the images
[I2, rect] = imcrop(imageArray);
%% Run Loop
for k = 1 : length(theFiles)
%Pulling in multiple Images from folder
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
imageArray = imread(fullFileName);
%% Cropping Image with Rect Cords
Icrop = imcrop(imageArray,rect);
%% convert to bw
BW = im2bw(Icrop, graythresh(Icrop));
%% Get Boundries
boundaries = bwboundaries(BW);
% putting in x and y cord
x = boundaries{1}(:, 2);
y = boundaries{1}(:, 1);
%% Curve Fit
%pulling up picture of x,y plot to determing where base of flame is
figure
plot(x, y)
% cutting off bottom of flame edge so we can do curve fit
prompt = '(******) cut off line = ';
cutnum = input(prompt);
indices = find(abs(y)>cutnum);
y(indices) = NaN;
y(indices) = [];
x(indices) = [];
%% Looking for repate x values, averaging them together, and replacing with 1 x value
uc1 = unique( x ) ;
mc2 = accumarray( x, y, [], #mean ) ;
newData = [uc1, mc2(uc1)];
%% splitting AA into two sperate matricies, i.e. x/y cordinates
y = newData(:,2);
x = newData(:,1);
%% Creating a line Based off of the averaged x and y cords
[p,S] = polyfit(x,y,4); % 4th degree polynomail
line{k} = p;
rValue{k} = S.normr;
Rsquared{k}=1-S.normr^2/norm(y-mean(y))^2;
close all
clear BW
clear x
clear y
end
I would like to make some calculation on my Raman spectra, I have a problem to read my input file. My file .txt contain 2 columns: X = Wavelength (cm-1) and Y = Raman intensity. The name of the file contains the coordinates of the position or the Raman spectrum was collected, for example (0.00,-05.00) or (-2.00,-0.50).
function Read_Raman_Files
% Reads Raman spectra from txt files.
% Each file contains the data for a single Raman spectrum:
% X = Wavelength (cm-1)
% Y = Raman intensity
% The name of the input file contains the coordinates at which the spectrum is taken.
% Results are stored in 'data.mat'.
files = dir('-5.0,0.00.txt');
Ncurves = length(files);
if Ncurves==0, display('No txt files found!'); return; end
for i = 1:Ncurves,
i
fname = files(i).name;
data = importdata(fname);
if i==1, X = data(:,i); end
Y(:,i) = data(:,2);
dash = strfind(fname,'__');
Xpos(i) = str2num(fname(strfind(fname,'Xµm_')+4:dash(2)-1));
Ypos(i) = str2num(fname(strfind(fname,'Yµm_')+4:dash(3)-1));
end;
save('data.mat', 'Ncurves', 'X', 'Y', 'Xpos', 'Ypos');
return
Here is an example on how to read the content of a file that has 2 columns of integers separated by comma:
formatSpec = '%d%d';
[x, y] = textread('yourFile.txt', formatSpec, 'delimiter',',');
I have a text file, suppose myfile.txt, that stores floating point coordinates in following manner:
53
-464.000000 -20.000000 0.009000
-464.000000 -17.000000 0.042000
-464.000000 -13.000000 0.074000
-464.000000 -11.000000 0.096000
-464.000000 -8.000000 0.114000
...
...
...
42
380.000000 193.000000 7.076000
381.000000 190.000000 7.109000
383.000000 186.000000 7.141000
384.000000 184.000000 7.163000
384.000000 183.000000 7.186000
386.000000 179.000000 7.219000
...
...
...
the first line specifies the number of lines for the first set of coordinates, followed by that many lines. and then theres is another integer specifying the the number of lines for the next set of coordinates.
i.e. 1st line has 53, so next 53 lines are 1st set of coords(ending at line 54). Then line 55 has value 42, so next 42 lines are 2nd set of coords.
How can I read the text file such that i read 1st line, and the next 53 lines are read and stored in matrix. Then read 42 and the next 42 lines are read and stored? The text file is like this until EOF.
You could do it with Low-Level File I/O, like this:
fid = fopen('myfile.txt', 'r');
matrix = {};
while ~feof(fid)
N = fscanf(fid, '%d\n', 1);
matrix{end + 1} = fscanf(fid, '%f\n', [3 N])';
end
fclose(fid);
fid = fopen('myfile.txt ', 'r');
ind = 1;
mycell = {};
while ~feof(fid)
N = fscanf(fid, '%d\n', 1);
matrix = fscanf(fid, '%f\n', [3 N]);
matrix = transpose(matrix);
cell{ind} = matrix;
ind = ind + 1;
end
fclose(fid);
My two cents:
function matrix_cells = import_coordinates(filename, startRow, endRow)
%IMPORT_COORDINATES Import numeric data from a text file as a matrix.
% MATRIX_CELLS = IMPORT_COORDINATES(FILENAME) Reads data from text file FILENAME
% for the default selection and store it into the array of cells MATRIX_CELLS.
%
% MATRIX_CELLS = IMPORT_COORDINATES(FILENAME, STARTROW, ENDROW) Reads data from
% rows STARTROW through ENDROW of text file FILENAME.
%
% Example:
% matrix_cells = import_coordinates('coordinates.txt', 1, 15);
%
% See also TEXTSCAN.
%% Initialize variables.
delimiter = ' ';
if nargin<=2
startRow = 1;
endRow = inf;
end
%% Format string for each line of text:
% column1: double (%f)
% column2: double (%f)
% column3: double (%f)
% For more information, see the TEXTSCAN documentation.
formatSpec = '%f%f%f%[^\n\r]';
%% Open the text 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, 'MultipleDelimsAsOne', true, 'EmptyValue' ,NaN,'HeaderLines', startRow(1)-1, 'ReturnOnError', false);
for block=2:length(startRow)
frewind(fileID);
dataArrayBlock = textscan(fileID, formatSpec, endRow(block)-startRow(block)+1, 'Delimiter', delimiter, 'MultipleDelimsAsOne', true, 'EmptyValue' ,NaN,'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);
%% Post processing for unimportable data.
% No unimportable data rules were applied during the import, so no post
% processing code is included. To generate code which works for
% unimportable data, select unimportable cells in a file and regenerate the
% script.
%% Create output variable
matrix_temp = [dataArray{1:end-1}];
%% MY CONTRIBUTION ;)
% find rows with nan values
[i,~]=find(isnan(matrix_temp));
indexes=unique(i);
%% Create output cell array
matrix_cells=cell(numel(indexes),1);
%% Fill cell array filtering out unuseful rows and parting original matrix
for i=1:1:numel(indexes)-1
matrix_cells{i}=matrix_temp(indexes(i)+1:indexes(i+1)-1,:);
end
% Last matrix
matrix_cells{end}=matrix_temp(indexes(end)+1:size(matrix_temp,1),:);
The first part (reading and import of text file) of this function was autogenerated by MATLAB. I added only the code to split and save the matrix into a cell array.
I assume that coordinates do not contain nan values; morevoer, I do not check for consistency between the declared number of rows and the actual one for each block.
Here an example; the following is the file coordinates.txt
5
-464.000000 -20.000000 0.009000
-464.000000 -17.000000 0.042000
-464.000000 -13.000000 0.074000
-464.000000 -11.000000 0.096000
-464.000000 -8.000000 0.114000
3
380.000000 193.000000 7.076000
381.000000 190.000000 7.109000
383.000000 186.000000 7.141000
2
384.000000 184.000000 7.163000
384.000000 183.000000 7.186000
1
386.000000 179.000000 7.219000
Excute the function:
coordinate_matrices=import_coordinates('coordinates.txt')
coordinate_matrices =
[5x3 double]
[3x3 double]
[2x3 double]
[1x3 double]
This is the content of every cell:
>> coordinate_matrices{1}
ans =
-464.0000 -20.0000 0.0090
-464.0000 -17.0000 0.0420
-464.0000 -13.0000 0.0740
-464.0000 -11.0000 0.0960
-464.0000 -8.0000 0.1140
>> coordinate_matrices{2}
ans =
380.0000 193.0000 7.0760
381.0000 190.0000 7.1090
383.0000 186.0000 7.1410
>> coordinate_matrices{3}
ans =
384.0000 184.0000 7.1630
384.0000 183.0000 7.1860
>> coordinate_matrices{4}
ans =
386.0000 179.0000 7.2190
I am working with this code to import a spread sheet containing x (angle) and y (intensity) axes and plot the results:
%% Initialize variables
filename = ['C:\Users\themosawi\Desktop\khalifadec2104\mih10distracks-azi\mih10-distal-2_5mmbelowDH\mih10distracks_0363.dat'];
startRow = 5;
%% Format string for each line of text:
formatSpec = '%14f%f%[^\n\r]';
%% Open the text file.
fileID = fopen(filename,'r');
%% Read columns of data according to format string.
dataArray = textscan(fileID, formatSpec, 'Delimiter', '', 'WhiteSpace', '', 'HeaderLines' ,startRow-1, 'ReturnOnError', false);
%% Close the text file.
fclose(fileID);
%% Allocate imported array to column variable names
angle = dataArray{:, 1};
intensity = dataArray{:, 2};
%% Plot
plot(angle, intensity)
Sometimes I get plots looking like this (in which the plotting starts at the middle of the peak):
I would like the plotting to start from angle 45 instead of angle 0 (in the x-axis) so I can identify both peaks.
Here is the file:
http://mosawi.co.uk/mih10distracks_0363.dat
Try the code below, it may work:
%% Initialize variables
filename = ['C:\Users\themosawi\Desktop\khalifadec2104\mih10distracks-azi\mih10-distal-2_5mmbelowDH\mih10distracks_0363.dat.txt'];
startRow = 5;
%% Format string for each line of text:
formatSpec = '%14f%f%[^\n\r]';
%% Open the text file.
fileID = fopen(filename,'r');
%% Read columns of data according to format string.
dataArray = textscan(fileID, formatSpec, 'Delimiter', '', 'WhiteSpace', '', 'HeaderLines' ,startRow-1, 'ReturnOnError', false);
%% Close the text file.
fclose(fileID);
%% Allocate imported array to column variable names
angle = dataArray{:, 1};
intensity = dataArray{:, 2};
%% New angle & Intensity
lap = 100; % // Enter any angle value in range of your angle data
ind = find(angle > lap -.4 & angle < lap + .6);
newAngle = linspace(angle(ind), angle(ind)+angle(end), length(angle));
newIntensity = [intensity(ind:end); intensity(1:ind-1)];
%% Plot
plot(newAngle, newIntensity)
What I got as output:
Here's one approach, working with the cyclical nature of the data:
angle = angle(:).'; % ensures that these are both row
intensity = intensity(:).'; % vectors (may be unnecessary)
plot( [ angle-360 angle angle+360 ], [ intensity intensity intensity ] )
xlim( [0 360] + 45 ) % or +/- whatever you want
I'm not 100% sure I got your question right, but this is what I would do to phase shift this plot:
If so, you could replace:
%% Plot
plot(angle, intensity)
with
%% Plot
shift_angle = 45;
plot(angle, [intensity(shift_angle:end) ; intensity(1:shift_angle-1)])
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(...)