I am reading the input of the MATLAB code from an Excel file using xlsread, after the calculation I am exporting to Word for writing out the report(writetoword.m). In Excel, there is a string which I should read in MATLAB and output in Word.
In Excel file(input.xlsx) it is written 'shoe'.
I read using
[num,txt,raw] = xlsread('input.xlsx');
eng = txt(14,19); % the word 'shoe' in that excel cell
In writetoword.m, I wrote,
test = eng;
WordText(ActXWord,test,Style,[0,1]);
function WordText(actx_word_p,text_p,style_p,enters_p,color_p)
if(enters_p(1))
actx_word_p.Selection.TypeParagraph;
end
actx_word_p.Selection.Style = style_p;
if(nargin == 5)
actx_word_p.Selection.Font.Color=color_p;
end
actx_word_p.Selection.TypeText(text_p);
actx_word_p.Selection.Font.Color='wdColorAutomatic';
for k=1:enters_p(2)
actx_word_p.Selection.TypeParagraph;
end
return
It is not printing anything. The error is in the line
actx_word_p.Selection.TypeText(text_p);
now if I write
test = 'eng';
WordText(ActXWord,test,Style,[0,1]);
It will come as eng and not shoe.
How can I fix this problem?
You're assigning txt as a cell instead of a string. Use eng = txt{14,19}; instead.
Related
I am trying to concatenate three lines (I want to leave the lines as is; 3 rows) from Shakespeare.txt file that shows:
To be,
or not to be:
that is the question.
My code right now is
fid = fopen('Shakespeare.txt')
while ~feof(fid)
a = fgets(fid);
b = fgets(fid);
c = fgets(fid);
end
fprintf('%s', strcat(a, b, c))
I'm supposed to use strcat and again, I want concatenated and leave them as three rows.
One method of keeping the rows separate is by storing the lines of the text file in a string array. Here a 1 by 3 string array is used. It may also be a good idea to use fgetl() which grabs each line of the text file at a time. Concantenating the outputs of fgetl() as strings may also be another option to ensure the they do not get stored as character (char) arrays. Also using the \n indicates to line break when printing the strings within the array String_Array.
fid = fopen('Shakespeare.txt');
while ~feof(fid)
String_Array(1) = string(fgetl(fid));
String_Array(2) = string(fgetl(fid));
String_Array(3) = string(fgetl(fid));
end
fprintf('%s\n', String_Array);
Ran using MATLAB R2019b
I want to read the data of a file with size about 60 MB into matlab in some variables, but I get errors. This is my code:
clear all ;
clc ;
% Reading Input File
Dataz = importdata('leak0.lis');
%Dataz = load('leak0.lis');
for k = 1:1370
foundPosition = 1 ;
for i=1:size(Dataz,1)
strp = sprintf('I%dz=',k);
fprintf(strp);
findValue = strfind(Dataz{i}, strp) ;
if ~isempty(findValue)
eval_param = strp + '(foundPosition) = sscanf(Dataz{i},''%*c%*c%*f%*c%*c%f'') ;';
disp(eval_param);
% str(foundPosition) = sscanf(Dataz{i},'%*c%*c%*f%*c%*c%f') ;
eval(eval_param);
foundPosition = foundPosition + 1 ;
end
end
end
When I debugged it, I found out that the dataz is empty & so it doesn't proceed to next lines. I replace it with fopen, load & etc, but it didn't work.
From the Matlab help files, import data is likely failing because it doesn't understand your file format.
From the help files
Name and extension of the file to import, specified as a string. If importdata recognizes the file extension, it calls the MATLAB helper function designed to import the associated file format (such as load for MAT-files or xlsread for spreadsheets). Otherwise, importdata interprets the file as a delimited ASCII file.
For ASCII files and spreadsheets, importdata expects to find numeric
data in a rectangular form (that is, like a matrix). Text headers can
appear above or to the left of the numeric data, as follows:
Assuming that your .lis files actually have delimited text.
You should adjust the delimiter in the importdata call so that Matlab can understand your file.
filename = 'myfile01.txt';
delimiterIn = ' ';
headerlinesIn = 1;
A = importdata(filename,delimiterIn,headerlinesIn);
how do I insert a string into a csv file in matlab. i used this code to write some data and create my csv file:
and here is the output of the code:
I'm trying to insert some text in the first 2 columns before the numerical data..
thanks in advance :)
There are several approaches are possible here.
Let's take a look at some of them:
If you need to add string to your csv file.
For example, I create some csv file like your:
q = [1 2 3 4 5 6 7];
csvwrite('csvlist4.csv',q,2,0);
All troubles is to add some string to csv - it's because we need to combine numeric and text data. There are no good functions for it in Matlab except low levels:
c = 'some big text';
fid = fopen('csvlist4.csv','r+');
fprintf(fid,c);
How it works: the data in csv is an array. I put data in 3rd row, so first and second is an empty but they have ','. When you use fprintf it will replace this , with your text. So if text is too long it will overwrite your data.
How to avoid this?
Easiest way - is to do it with help of xlswrite function. For your example:
txt = cell(size(Q))
txt{1} = 'this is your text'
X = [txt; num2cell(Q)]
xlswrite('new.xlsx',X)
Result:
Important moment here: number of cell for text must be the same as your data. But I filled with the text only first cell in my example.
And the one more way: read csv file, modify it's data and write to csv:
csvwrite('csvlist4.csv',a,2,0);
m = csvread('csvlist4.csv');
fid = fopen('csvlist4.csv','w');
c = 'your text'
fprintf(fid, c); fprintf(fid, '\n');
fclose(fid);
dlmwrite('csvlist4.csv', m(3:end,:), '-append');
Of course you can use cell array instead c and so on and so on.
Hope it helps!
I have the following to shuffle the letters in a word, but I need to change it into the form of a function that asks the user to input a word, and outputs the shuffled word.
How can I do this???
word = input('type a word to be scrambled: ', 's');
word(randperm(numel(word)))
The code you have to scramble the word is indeed correct. To do what you're asking, you really only have to make one change to the above code. Just place a function declaration in a .m file and call it something like scramble.m. Then do:
function word = scramble
word = input('type a word to be scrambled: ', 's');
word(randperm(numel(word)))
end
This should output the word as a string when you call the function. So save this file, then in the command prompt, type in:
>> word = scramble;
This should ask you for the word you want scrambled, scramble it and return the word. This word is stored in the variable word in the MATLAB workspace.
Some suggested reading for you: http://www.mathworks.com/help/matlab/ref/function.html
MathWorks is very good with their documentation and especially the syntax. Read the above link for further details on how you define a function and use it, but the gist of it is how I did it above.
The general format of a matlab function is
function output = MyFunctionName(input)
... code using 'input' goes here
end %
If you have multiple outputs, you place them inside an array, preferably separated by commas. If you have multiple inputs, you list them separated by commas:
function [out1, out2,...,outN] = MyFunctionName(input1, input2,...,inputN)
... code using the inputs goes here
end %
For your problem, you're not passing the word to the function so the call the function needs no input, BUT you need to input the word from inside the function. Here's one way to do it.
function word = ShuffleLetters
% Output: 'word' that is shuffled within function
% Input: none
word = input('type a word to be scrambled: ', 's');
word = word(randperm(numel(word)));
end
Here's a sample usage:
>> ShuffleLetters
type a word to be scrambled: bacon
ans =
bonca
Finally, inputs and outputs are optional. This m-function just prints 'Hi!'
function SayHi
disp('Hi!')
end
I have my sparse features stored in a text file in the following format (ArrayIndex: Value). Currently I am parsing the text using reg-expressions and converting this to a matlab array. What I wanted to know was, is there a faster/better more MATLAB-ish approach to convert this format of data into a matlab array.
2402:0.099061 2404:0.136546 2406:0.447161 2407:0.126333 2408:0.213803 2411:0.068189 2416:0.223526 2417:0.090420
EDIT:
You can read and parse the file using TEXTSCAN, then the build sparse matrix from those values:
fid = fopen('input.txt');
C = textscan(fid, '%f:%f');
fclose(fid);
C = sparse(1,C{1},C{2});
result:
>> C
C =
(1,2402) 0.099061
(1,2404) 0.13655
(1,2406) 0.44716
(1,2407) 0.12633
(1,2408) 0.2138
(1,2411) 0.068189
(1,2416) 0.22353
(1,2417) 0.09042