How to make a code to access the internet and other programs

 Accepted Answer

You can use system commands to do this. See system
You can also use the ! to do this.

6 Comments

So how can I set it up like so. My plan is to type in an code and the program would respond verbally and activate the function such as, open up internet explorer and search something, open up files like documents in word etc. or fine a file.
For some of that purpose, using open() is appropriate: that is pretty much the same as taking the given file and double-clicking on it to open it.
For other things you listed, you would need to use ActiveX to tell the program what to do -- if you specifically wanted to tell IE to perform its search logic. But if you were wanting to do a web search then you could use webread() or urlread() to send a query to Google or Bing.
To find a file, you can use system() to launch dir() with the /s option.
Ok could you give me a code example because that was really confusing. I have a code like so:
command=input('What is your command');
if strcmpi(command,'Open someting')
NET.addAssembly('System.Speech');
obj = System.Speech.Synthesis.SpeechSynthesizer;
obj.Volume = 100;
Speak(obj, 'Opening something');
end
And I open the internet and whatever. So how could I implement what you told me to my code. Please
NET.addAssembly('System.Speech');
obj = System.Speech.Synthesis.SpeechSynthesizer;
obj.Volume = 100;
command = strtrim( input('What is your command', 's') );
parts = regexp(command, '\s+', 'split');
text_to_say = 'Unrecognized command';
if isempty(parts)
Speak(obj, text_to_say);
else
switch lower(parts)
case 'open'
if length(parts) == 2
filename = parts{2};
if exist(filename, 'file')
text_to_say = 'Opening file';
Speak(obj, text_to_say);
open(filename);
else
text_to_say = 'Could not find file to open it';
Speak(obj, text_to_say);
end
else
Speak(obj, text_to_say);
end
case 'search'
if length(parts) == 2
filename = parts{2};
cmd = sprintf('cd C:\; dir /s "%s"', filename);
text_to_say = 'Searching disk C for file';
Speak(obj, text_to_say);
[status, message] = system(cmd);
if isempty(message)
text_to_say = 'System had no response to search request';
Speak(obj, text_to_say);
else
text_to_say = 'System result of search follows';
Speak(obj, text_to_say);
Speak(obj, message);
end
else
Speak(obj, text_to_say');
end
otherwise
Speak(obj, text_to_say);
end
end
Ok Walter your going to have to walk me throught this cause I have no idea what any of this means haha I ran this code but it keeps giving error:
SWITCH expression must be a scalar or a character vector.
Error in Practice (line 11)
switch lower(parts)
Change
switch lower(parts)
to
switch lower(parts{1})

Sign in to comment.

More Answers (1)

ok well no error
What is your command parts
Undefined function or variable 'text_to_say'.
Error in Practice (line 45)
Speak(obj, text_to_say);

15 Comments

Did you perhaps accidentally delete the
text_to_say = 'Unrecognized command';
that is near the beginning?
No it is still there.
NET.addAssembly('System.Speech');
obj = System.Speech.Synthesis.SpeechSynthesizer;
obj.Volume = 100;
command = strtrim( input('What is your command ', 's') );
parts = regexp(command, '\s+', 'split');
if isempty(parts)
text_to_say = 'Unrecognized command';
Speak(obj, text_to_say);
else
switch lower(parts{1})
case 'open'
if length(parts) == 2
filename = parts{2};
if exist(filename, 'file')
text_to_say = 'Opening file';
Speak(obj, text_to_say);
open(filename);
else
text_to_say = 'Could not find file to open it';
Speak(obj, text_to_say);
end
else
Speak(obj, text_to_say);
end
case 'search'
if length(parts) == 2
filename = parts{2};
cmd = sprintf('cd C:\; dir /s "%s"', filename);
text_to_say = 'Searching disk C for file';
Speak(obj, text_to_say);
[status, message] = system(cmd);
if isempty(message)
text_to_say = 'System had no response to search request';
Speak(obj, text_to_say);
else
text_to_say = 'System result of search follows';
Speak(obj, text_to_say);
Speak(obj, message);
end
else
Speak(obj, text_to_say');
end
otherwise
Speak(obj, text_to_say);
end
end
Sorry I took so long my computer was damage and had to get a new.
My code had
parts = regexp(command, '\s+', 'split');
text_to_say = 'Unrecognized command';
Your code moves the second of those two lines to the isempty() case only, leaving it undefined for the cases of a recognized verb followed by something unrecognized, and for the case of an unrecognized verb.
Hello Walter, Thank you so much for this. So now I'm trying figure out how to use this code. So after I run it what is the next step? Like what command will open up the internet or any of browsers?
You were not able to locate the executable for Internet Explorer, or for Chrome? Which Windows are you using and which browser do you want to be able to open?
Both if possible. Like I would like to make a code where I ask to open whatever program i choose. Im hoping to make the code like so:
funtion
command=input('What is your command ','s');
elseif strcmpi(res,'Open Program')
%The word Program is just a general example
NET.addAssembly('System.Speech');
obj = System.Speech.Synthesis.SpeechSynthesizer;
obj.Volume = 100;
Speak(obj, 'Opening Program')
Then I believe the rest of the code will follow.
I still need to know your Windows version to figure out where IE and Chrome would have been stashed.
Hey Walter sorry about the long time I have taking.
I am using Windows 10.
On my Windows 10 system:
  • Firefox: C:\Program Files\Mozilla Firefox\firefox.exe
  • Internet Explorer: C:\Program Files (x86)\Internet Explorer\iexplore.exe
  • Chrome: C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
If you need to access these by program, they are in the registry at HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\App Paths\ under firefox.exe\ (Default), IEXPLORE.EXE\ (Default), and chrome.exe\ (Default)
Ok so do I put that in the code from above or is this a who new code?
Could I get like an example with your directory?
%initializations. No need to keep doing these.
chromelocation = 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe';
local_file_prefix = 'file://';
NET.addAssembly('System.Speech');
obj = System.Speech.Synthesis.SpeechSynthesizer;
obj.Volume = 100;
%then inside loop
....
else
word1 = regexp(res, '\S+', 'match', 'once');
if strcmpi(word1, 'Open')
openwhat = regexprep(res, '\s*\S+\s*', '', 'once');
uri = regexp(openwhat, '^\w\w+:'); %with at least two characters before the :
if isempty(uri)
openwhat = [local_file_prefix openwhat];
end
text_to_say = sprintf('Opening %s', openwhat);
Speak(obj, text_to_say)
cmd = sprintf('%s "%s"', chromelocation, openwhat);
else
...
Could I get like an example with your directory?
Sorry, I am not running Windows most of the time, and I am having a lot of trouble with my virtual machine software these days.
Well my chrome is in the same location. But i really dont know what im really looking at lol.
the dots before and after else i never seen them in a code before. In all the codes that we have gone over where is this suppose to fit?
%initializations. No need to keep doing these.
chromelocation = 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe';
local_file_prefix = 'file://';
NET.addAssembly('System.Speech');
obj = System.Speech.Synthesis.SpeechSynthesizer;
obj.Volume = 100;
command = strtrim( input('What is your command ', 's') );
parts = regexp(command, '\s+', 'split');
if isempty(parts)
text_to_say = 'Unrecognized command';
Speak(obj, text_to_say);
else
switch lower(parts{1})
case 'open'
if length(parts) == 2
cmd = [];
filename = parts{2};
if exist(filename, 'file')
openwhat = [local_file_prefix filename];
text_to_say = 'Opening local file';
else
uri = regexp(filename, '^\w\w+:'); %with at least two characters before the :
if isempty(uri)
text_to_say = 'Local file not found';
filename = [];
else
text_to_say = 'Opening URL with browser.';
cmd = sprintf('%s "%s" &', chromelocation, filename);
filename = [];
end
end
Speak(obj, text_to_say);
if ~isempty(filename)
open(filename);
end
if ~isempty(cmd)
cmd = sprintf('%s "%s" &', chromelocation, openwhat);
system(cmd);
end
else
Speak(obj, text_to_say);
end
case 'search'
if length(parts) == 2
filename = parts{2};
cmd = sprintf('cd C:\; dir /s "%s"', filename);
text_to_say = 'Searching disk C for file';
Speak(obj, text_to_say);
[status, message] = system(cmd);
if isempty(message)
text_to_say = 'System had no response to search request';
Speak(obj, text_to_say);
else
text_to_say = 'System result of search follows';
Speak(obj, text_to_say);
Speak(obj, message);
end
else
Speak(obj, text_to_say');
end
otherwise
Speak(obj, text_to_say);
end
end

Sign in to comment.

Categories

Find more on Environment and Settings in Help Center and File Exchange

Products

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!